English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
varA statement declares a variable and chooses whether to initialize it with a value.
Variables are containers used to store information.
Creating a variable in JavaScript is called "declaring" a variable:
var city;
After declaration, the variable is empty (has no value).
To assign a value to a variable, use the equal sign (=):
city = "New Delhi";
You can also assign a value to a variable when you declare it:
var city = "New Delhi";
Var declarations, regardless of where they appear, are processed before any code is executed. This is calledHoisting.
If you redefine a JavaScript variable, it will not lose its value.
You can find ourJavaScript variable tutorialandIn the JavaScript scope tutorialLearn more about variables.
var identifier = value;
var city = "New Delhi";Test and see‹/›
All browsers fully support the var statement:
Statement | |||||
var | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
identifier | Specify the name of the variable. It can be any legal identifier. Variable names can contain letters, numbers, underscores, and dollar signs.
|
value | The initial value of a variable. It can be any legal expression. Default valueundefined |
JavaScript Version: | ECMAScript 1 |
---|
Declare and initialize three variables:
var x = 10, y = 15, z = 5;Test and see‹/›
Initialize several variables:
var x, y, z;// Variable Declaration x = y = z = 10; // Initialize VariablesTest and see‹/›
JavaScript Tutorial:JavaScript Variables
JavaScript Tutorial:JavaScript Scope