VARIABLES
A variable is a temporary storage container that holds information, or data . When you create a variable, you give it a name. You use the name to access the information that the variable contains. After you create a variable, you can put new information in it as often as you want.
You create a variable by declaring it. When you
declare a variable, it's empty. Its value at this point is undefined . To declare a
variable, use a var statement:
var a;
You can declare more than one variable at a time with the same
var statement:
var a, b, c;
Name Variables in JavaScript
There are a few rules for naming JavaScript variables, such as the following:
- no spaces, hyphens, or punctuation characters (except for underscores)
- must start with a letter or an underscore
- no more than 255 characters (whew!)
- can't use JavaScript reserved words (for example, you can't name a variable var)
It also helps to give your variables meaningful names so you can remember what the variables are used for.
JavaScript is case sensitive, so pay attention to the case of variable names;
myName is not the same as myname,
MyName, or MYNAME.
After you declare a variable, you can assign a value to it.
a = 10; b = bolo;
After you've declared a variable, you don't need to use var to
refer to the variable.
The variable name is on the left side of the equal sign, and the variable value is on the right side of the equal sign. The equal sign is called the assignment operator . You'll learn more about operators later in this lesson.
Variable assignment works from right to left. The first code statement above
assigns a value of 10 to the variable named a.
Variables can also be initialized . When you initialize a variable, you assign a value at the same time you declare the variable.
var a = 10; var b = bolo;
