JavaScript Variable

Variable means anything that can vary. JavaScript includes variables which hold the data value and it can be changed anytime.

JavaScript variables are containers for storing data values.

In this example, x, y, and z, are variables, declared with the var keyword:

There are some rules while declaring a JavaScript variable (also known as identifiers).

  1. Name must start with a letter (a to z or A to Z), underscore( _ ), or dollar( $ ) sign.
  2. After first letter we can use digits (0 to 9), for example value1.
  3. JavaScript variables are case sensitive, for example x and X are different variables.


var x = 5;
var y = 6;
var z = x + y;

Example -

One Statement, Many Variables

You can declare many variables in one statement.

Start the statement with var and separate the variables by comma:

var person = "John Doe",
carName = "Volvo",
price = 200;

Example -

Re-Declaring JavaScript Variables

If you re-declare a JavaScript variable, it will not lose its value.

The variable carName will still have the value "Volvo" after the execution of these statements:


var carName = "Volvo";
var carName;

Example -