English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript var statement

 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.

Syntax:

var identifier = value;
var city = "New Delhi";
Test and see‹/›

Browser compatibility

All browsers fully support the var statement:

Statement
varIsIsIsIsIs

Parameter Value

ParameterDescription
identifierSpecify the name of the variable. It can be any legal identifier.
Variable names can contain letters, numbers, underscores, and dollar signs.
  • Variable names must start with a letter

  • Variable names can also start with $ and _

  • Variable names are case-sensitive (city and City are different variables)

  • Reserved words cannot be used as variable names

valueThe initial value of a variable. It can be any legal expression. Default valueundefined

Technical Details

JavaScript Version:ECMAScript 1

More Examples

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 Variables
Test and see‹/›

Also check out

JavaScript Tutorial:JavaScript Variables

JavaScript Tutorial:JavaScript Scope

 JavaScript Statements and Variable Declarations