JavaScript varStatement
JavaScript Statement Reference
Examples
Create a variable named carName with the value "Volvo":
Try it Yourself »
More examples are included at the bottom of this article.
Definition and Usage
The var statement is used to declare variables.
Creating a JavaScript variable is also called "declaring" a variable:
After a variable is declared, the variable is empty (has no value).
To assign a value to a variable, do the following:
You can also assign a value to a variable when you declare it:
For more information about variables, please refer to ourJavaScript VariablesTutorialJavaScript ScopeTutorial.
Browser Support
| Statement | |||||
|---|---|---|---|---|---|
| var | Yes | Yes | Yes | Yes | Yes |
Syntax
Parameter Values
| Parameter | Description |
|---|---|
| varname | Required. Specifies the variable name. Variable names can contain letters, digits, underscores, and dollar signs.
|
| value | Optional. Specifies the value of the variable. Note:If a variable is declared without a specified value, its default value isundefined |
Technical Details
| JavaScript Version: | 1.0 |
|---|

More Examples
Examples
Create two variables x and y. Assign 5 to x and 6 to y. Then output the result of x + y:
var y = 6;
document.getElementById("demo").innerHTML = x + y;
Try it Yourself »
Examples
You can declare multiple variables in one statement.
The statement begins withvarand variables are separated by commas:
age = 30,
job = "carpenter";
Try it Yourself »
Examples
Use variables in a loop:
var i;
for (i = 0; i < 5; i++) {
text += "The number is " + i + "<br>";
}
Try it Yourself »
Related Pages
JavaScript Tutorial:JavaScript Variables
JavaScript Tutorial:JavaScript Scope
JavaScript Statement Reference Other Extensions