JavaScript Statements Reference
Create a variable called carName and assign the value "Volvo" to it:
More "Try it Yourself" examples below.
The var statement declares a variable.
Variables are containers for storing information.
Creating a variable in JavaScript is called "declaring" a variable:
After the declaration, the variable is empty (it has no value).
To assign a value to the variable, use the equal sign:
You can also assign a value to the variable when you declare it:
For more information about variables, read our JavaScript Variables Tutorial and JavaScript Scope Tutorial.
Statement | |||||
---|---|---|---|---|---|
var | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
varname | Required. Specifies the name of the variable. Variable names can contain letters, digits, underscores, and dollar signs.
|
value | Optional. Specifies the value to assign to the variable. Note: A variable declared without a value will have the value undefined |
JavaScript Version: | 1.0 |
---|
Create two variables. Assign the number 5 to x, and 6 to y. Then display the result of x + y:
You can declare many variables in one statement.
Start the statement with var and separate the variables by comma:
Using variables in loops:
JavaScript Tutorial: JavaScript Variables
JavaScript Tutorial: JavaScript Scope