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).
var x = 5;
var y = 6;
var z = x + y;
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;
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;