JavaScript Variables Note: Because JavaScript is case-sensitive, variable names are case-sensitive. A variable is a place holder in your computer's memory for information that can change. This place in memory is given a name in JavaScript so that you can easily refer to it. Declaring a variable is easy. All you have to do is precede the variable name with the JavaScript reserved word var as follows: var myVariable Once the variable is declared, you can set it to any one of the values shown in the above table. Here is an example: Later on in the program you can change its value to something different: myVariable = 33 You can even change its type by simply assigning it a different type of value. For instance our variable was originally assigned a number. We can make it a string later in the program by simply assigning it one as follows: myVariable = "this is a string" Note that the variables in some other languages, such a C, are strongly typed and you are not allowed to randomly change the variable's type as we did here. var myVariable = "JavaScript is cool" This is method that you will probably use most of the time to declare and initialize your variables. myVariable = "this is not too cool" You will note that the variable name myVariable contains two words with the first letter of the second word capitalized. It is common practice in JavaScript to use small letters for variable names and to capitalize the first letter of every new word starting with the second word. Also note that there is no space between the two words.
As with algebra, JavaScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carname.
Rules for JavaScript variable names:
myVariable = 5
You can initialize a variable and assign it a value at the same time as follows:
You can even leave out the var keyword as follows: