[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/var [Back]  [Original]

var - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

var

Baseline

20157

var

var x = 1;

if (x === 1) {
  var x = 2;

  console.log(x);
  // : 2
}

console.log(x);
// : 2

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

JavaScript

valueN

undefined

var var

js
function foo() {
  var x = 1;
  function bar() {
    var y = 2;
    console.log(x); // 1 ( `bar`  `x` )
    console.log(y); // 2 (`y` )
  }
  bar();
  console.log(x); // 1 (`x` )
  console.log(y); // ReferenceError, `y`  `bar` 
}

foo();

try...catchswitchfor var var

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

var delete JavaScript delete

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

NodeJS CommonJS ECMAScript

var =

var

: var 1 HTML 2 <script> 2 2

js
bla = 2;
var bla;

js
var bla;
bla = 2;

undefined

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

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

var

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

var function var

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

var letconstclassimport

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

var

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

let var

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

var

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

foo(2); // Logs 1

catch var catch catch catch catch

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

2

js
var a = 0,
  b = 0;

2 1

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

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

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

x y "x = y" y ReferenceError undefined x undefined y 'A'

var x = y = 1 y y = 1

js
var x = 0;
function f() {
  var x = y = 1; // x y 
}
f();

console.log(x, y); // 0 1

// :
// x 
// y 

js
"use strict";

var x = 0;
function f() {
  var x = y = 1; //  ReferenceError 
}
f();

console.log(x, y);

js
var x = 0; // x  0 

console.log(typeof z); // z  "undefined" 

function a() {
  var y = 2; // y  a  2 

  console.log(x, y); // 0 2

  function b() {
    x = 3; //  x  3 
    y = 4; //  y  4 
    z = 5; //  z  5 
    //  ReferenceError 
  }

  b(); //  z 
  console.log(x, y, z); // 3 4 5
}

a(); // b 
console.log(x, z); // 3 5
console.log(typeof y); // y  a  "undefined" 

=

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

ECMAScript 2027 LanguageSpecification
# sec-variable-statement


Web Proxy Viewer  |  New URL  |  Original Page