A variable is simply a place in the computer's memory to store information. All variables are referred to by the unique name you assigned to them.
In this example, x, y, and z, are variables:
var x = 5; var y = 6; var z = x + y;
- Identifiers can be short names (like x and y), or more descriptive names (age, sum, totalVolume).
- The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter
- Names can also begin with $ and _ (but we will not use it in this tutorial)
- Names are case sensitive (y and Y are different variables)
After the declaration, the variable has no value. (Technically it has the value of undefined)
To assign a value to the variable, use the equal sign:
carName = "Volvo";
You can also assign a value to the variable when you declare it: