| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/var | [Back] [Original] |
Get to know MDN better
var x = 1;
if (x === 1) {
var x = 2;
console.log(x);
// : 2
}
console.log(x);
// : 2
var name1;
var name1 = value1;
var name1 = value1, name2 = value2;
var name1, name2 = value2;
var name1 = value1, name2, /* , */ nameN = valueN;
var var
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
for (var a of [1, 2, 3]);
console.log(a); // 3
var delete JavaScript delete
"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 1 HTML 2 <script> 2 2
bla = 2;
var bla;
var bla;
bla = 2;
undefined
function doSomething() {
console.log(bar); // undefined
var bar = 111;
console.log(bar); // 111
}
function doSomething() {
var bar;
console.log(bar); // undefined
bar = 111;
console.log(bar); // 111
}
var
var a = 1;
var a = 2;
console.log(a); // 2
var a;
console.log(a); // 2; not undefined
var function var
var a = 1;
function a() {}
console.log(a); // 1
var a = 1;
let a = 2; // SyntaxError: Identifier 'a' has already been declared
var
let a = 1;
{
var a = 1; // SyntaxError: Identifier 'a' has already been declared
}
let var
var a = 1;
{
let a = 2;
}
var
function foo(a) {
var a = 1;
console.log(a);
}
foo(2); // Logs 1
catch var catch catch catch catch
try {
throw new Error();
} catch (e) {
var e = 2; // Works
}
console.log(e); // undefined
var a = 0,
b = 0;
var a = "A";
var b = a;
var a, b = a = "A";
var x = y,
y = "A";
console.log(x, y); // undefined A
x y "x = y" y ReferenceError undefined x undefined y 'A'
var x = 0;
function f() {
var x = y = 1; // x y
}
f();
console.log(x, y); // 0 1
// :
// x
// y
"use strict";
var x = 0;
function f() {
var x = y = 1; // ReferenceError
}
f();
console.log(x, y);
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"
=
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 |