Category: Variables
Declares and assigns an initial string to a variable.
Many apps process textual data. To be able to process string data your apps need to keep track of the strings in memory. Variables are simply names you use to refer to stored data in your apps. You can name your variables whatever you want so long as the name is not already used by the system. Variable names can not have spaces or special characters. In practice, it is helpful to name your variables in a way that describes the value they store. For instance, if the variable you create is to store a person's name you might name that variable personName.
To process data in our apps we need to assign values to memory locations we have previously named using var to declare a variable. Programmers read the statement 'state = "Illinois";' as 'state gets the string "Illinois"'. The variable getting the value always goes on the left hand side of the assignment operator =. The right hand side of the assignment operator can be a number or a string, or the number or string returned by a function, or the numeric or string result of the evaluation of an expression.
// Pioneering computer scientist. var name = "Alan Turing"; var birthYear = 1912; console.log(name + " was born in the year " + birthYear);
Example: Flip your name Convert a name to Last, First format.
// Convert a name to Last, First. var myName = prompt("Enter your first name and last name"); var blankIndex = myName.indexOf(' '); var lastFirst = myName.substring(blankIndex,myName.length) + ", " + myName.substring(0, blankIndex); console.log(lastFirst);
var str = "___";
Name | Type | Required? | Description |
---|---|---|---|
str | variable name | Yes | The name you will use in the program to reference the variable. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _. |
"___" | anything | Yes | The right hand side of the assignment operator can be any string, or the string returned by a function, or the string result of the evaluation of an expression. |
No return value. Variable created in memory and string value assigned.
Found a bug in the documentation? Let us know at documentation@code.org
var x = "__";
Found a bug in the documentation? Let us know at documentation@code.org