Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

var

BaselineWidely available

Thevar statement declares function-scoped or globally-scoped variables, optionally initializing each to a value.

Try it

var x = 1;if (x === 1) {  var x = 2;  console.log(x);  // Expected output: 2}console.log(x);// Expected output: 2

Syntax

js
var name1;var name1 = value1;var name1 = value1, name2 = value2;var name1, name2 = value2;var name1 = value1, name2, /* …, */ nameN = valueN;
nameN

The name of the variable to declare. Each must be a legal JavaScriptidentifier or adestructuring binding pattern.

valueNOptional

Initial value of the variable. It can be any legal expression. Default value isundefined.

Description

The scope of a variable declared withvar is one of the following curly-brace-enclosed syntaxes that most closely contains thevar statement:

Or if none of the above applies:

  • The currentmodule, for code running in module mode
  • The global scope, for code running in script mode.
js
function foo() {  var x = 1;  function bar() {    var y = 2;    console.log(x); // 1 (function `bar` closes over `x`)    console.log(y); // 2 (`y` is in scope)  }  bar();  console.log(x); // 1 (`x` is in scope)  console.log(y); // ReferenceError, `y` is scoped to `bar`}foo();

Importantly, other block constructs, includingblock statements,try...catch,switch, headers ofone of thefor statements, do not create scopes forvar, and variables declared withvar inside such a block can continue to be referenced outside the block.

js
for (var a of [1, 2, 3]);console.log(a); // 3

In a script, a variable declared usingvar is added as a non-configurable property of the global object. This means its property descriptor cannot be changed and it cannot be deleted usingdelete. JavaScript has automatic memory management, and it would make no sense to be able to use thedelete operator on a global variable.

js
"use strict";var x = 1;Object.hasOwn(globalThis, "x"); // truedelete globalThis.x; // TypeError in strict mode. Fails silently otherwise.delete x; // SyntaxError in strict mode. Fails silently otherwise.

In both NodeJSCommonJS modules and nativeECMAScript modules, top-level variable declarations are scoped to the module, and are not added as properties to the global object.

The list that follows thevar keyword is called abinding list and is separated by commas, where the commas arenotcomma operators and the= signs arenotassignment operators. Initializers of later variables can refer to earlier variables in the list and get the initialized value.

Hoisting

var declarations, wherever they occur in a script, are processed before any code within the script is executed. Declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is calledhoisting, as it appears that the variable declaration is moved to the top of the function, static initialization block, or script source in which it occurs.

Note:var declarations are only hoisted to the top of the current script. If you have two<script> elements within one HTML, the first script cannot access variables declared by the second before the second script has been processed and executed.

js
bla = 2;var bla;

This is implicitly understood as:

js
var bla;bla = 2;

For that reason, it is recommended to always declare variables at the top of their scope (the top of global code and the top of function code) so it's clear which variables are scoped to the current function.

Only a variable's declaration is hoisted, not its initialization. The initialization happens only when the assignment statement is reached. Until then the variable remainsundefined (but declared):

js
function doSomething() {  console.log(bar); // undefined  var bar = 111;  console.log(bar); // 111}

This is implicitly understood as:

js
function doSomething() {  var bar;  console.log(bar); // undefined  bar = 111;  console.log(bar); // 111}

Redeclarations

Duplicate variable declarations usingvar will not trigger an error, even in strict mode, and the variable will not lose its value, unless the declaration has an initializer.

js
var a = 1;var a = 2;console.log(a); // 2var a;console.log(a); // 2; not undefined

var declarations can also be in the same scope as afunction declaration. In this case, thevar declaration's initializer always overrides the function's value, regardless of their relative position. This is because function declarations are hoisted before any initializer gets evaluated, so the initializer comes later and overrides the value.

js
var a = 1;function a() {}console.log(a); // 1

var declarations cannot be in the same scope as alet,const,class, orimport declaration.

js
var a = 1;let a = 2; // SyntaxError: Identifier 'a' has already been declared

Becausevar declarations are not scoped to blocks, this also applies to the following case:

js
let a = 1;{  var a = 1; // SyntaxError: Identifier 'a' has already been declared}

It does not apply to the following case, wherelet is in a child scope ofvar, not the same scope:

js
var a = 1;{  let a = 2;}

Avar declaration within a function's body can have the same name as a parameter.

js
function foo(a) {  var a = 1;  console.log(a);}foo(2); // Logs 1

Avar declaration within acatch block can have the same name as thecatch-bound identifier, but only if thecatch binding is a simple identifier, not a destructuring pattern. This is adeprecated syntax and you should not rely on it. In this case, the declaration is hoisted to outside thecatch block, but any value assigned within thecatch block is not visible outside.

js
try {  throw new Error();} catch (e) {  var e = 2; // Works}console.log(e); // undefined

Examples

Declaring and initializing two variables

js
var a = 0,  b = 0;

Assigning two variables with single string value

js
var a = "A";var b = a;

This is equivalent to:

js
var a, b = a = "A";

Be mindful of the order:

js
var x = y,  y = "A";console.log(x, y); // undefined A

Here,x andy are declared before any code is executed, but the assignments occur later. At the timex = y is evaluated,y exists so noReferenceError is thrown and its value isundefined. So,x is assigned the undefined value. Then,y is assigned the value"A".

Initialization of several variables

Be careful of thevar x = y = 1 syntax —y is not actually declared as a variable, soy = 1 is anunqualified identifier assignment, which creates a global variable in non-strict mode.

js
var x = 0;function f() {  var x = y = 1; // Declares x locally; declares y globally.}f();console.log(x, y); // 0 1// In non-strict mode:// x is the global one as expected;// y is leaked outside of the function, though!

The same example as above but with a strict mode:

js
"use strict";var x = 0;function f() {  var x = y = 1; // ReferenceError: y is not defined}f();console.log(x, y);

Implicit globals and outer function scope

Variables that appear to be implicit globals may be references to variables in an outer function scope:

js
var x = 0; // Declares x within file scope, then assigns it a value of 0.console.log(typeof z); // "undefined", since z doesn't exist yetfunction a() {  var y = 2; // Declares y within scope of function a, then assigns it a value of 2.  console.log(x, y); // 0 2  function b() {    x = 3; // Assigns 3 to existing file scoped x.    y = 4; // Assigns 4 to existing outer y.    z = 5; // Creates a new global variable z, and assigns it a value of 5.    // (Throws a ReferenceError in strict mode.)  }  b(); // Creates z as a global variable.  console.log(x, y, z); // 3 4 5}a(); // Also calls b.console.log(x, z); // 3 5console.log(typeof y); // "undefined", as y is local to function a

Declaration with destructuring

The left-hand side of each= can also be a binding pattern. This allows creating multiple variables at once.

js
const result = /(a+)(b+)(c+)/.exec("aaabcc");var [, a, b, c] = result;console.log(a, b, c); // "aaa" "b" "cc"

For more information, seeDestructuring.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-variable-statement

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp