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;


A declaration can span multiple lines:


var person = "John Doe",

carName = "Volvo",

price = 200;


Value = undefined

In computer programs, variables are often declared without a value. The value can be something that has to be calculated, or something that will be provided later, like user input.

A variable declared without a value will have the value undefined.

The variable carName will have the value undefined after the execution of this statement:


Example

var carName;


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:


Example

var carName = "Volvo";

var carName;


JavaScript Arithmetic

As with algebra, you can do arithmetic with JavaScript variables, using operators like = and +:


Example

var x = 5 + 2 + 3;


You can also add strings, but strings will be concatenated:


Example

var x = "John" + " " + "Doe";


Also try this:


Example

var x = "5" + 2 + 3;

Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya