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;
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:
var carName;
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;
As with algebra, you can do arithmetic with JavaScript variables, using operators like =
and +
:
var x = 5 + 2 + 3;
You can also add strings, but strings will be concatenated:
var x = "John" + " " + "Doe";
Also try this:
var x = "5" + 2 + 3;