< Javascript Tutorial

Javascript variables

← Previous Next →


JavaScript variables are containers for storing data values.
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;

JavaScript Identifiers

All JavaScript variables must be identified with unique names. These unique names are called identifiers.

  1. Identifiers can be short names (like x and y), or more descriptive names (age, sum, totalVolume).
  2. 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)
          - Reserved words (like JavaScript keywords) cannot be used as names
  3. JavaScript identifiers are case-sensitive.

Declaring (Creating) JavaScript Variables

Creating a variable in JavaScript is called "declaring" a variable.
You declare a JavaScript variable with the var keyword:

var carName;
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:

 

← Previous Next →