| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/let | [Back] [Original] |
Get to know MDN better
let x = 1;
if (x === 1) {
let x = 2;
console.log(x);
// : 2
}
console.log(x);
// : 1
let name1;
let name1 = value1;
let name1 = value1, name2 = value2;
let name1, name2 = value2;
let name1 = value1, name2, /* , */ nameN = valueN;
let let
switch try...catch for let var let
let
let globalThis
if (true) let a = 1; // SyntaxError: Lexical declaration cannot appear in a single-statement context
letconstclass (TDZ)
TDZ ReferenceError undefined
var undefined let var
{
// TDZ
console.log(bar); // "undefined"
console.log(foo); // ReferenceError: Cannot access 'foo' before initialization
var bar = 1;
let foo = 2; // TDZ foo
}
let TDZ
{
// TDZ
const func = () => console.log(letVar); // OK
// TDZ letVar `ReferenceError`
let letVar = 3; // TDZ letVar
func(); // TDZ
}
typeof ReferenceError
{
typeof i; // ReferenceError: Cannot access 'i' before initialization
let i = 10;
}
typeof undefined
console.log(typeof undeclaredVariable); // "undefined"
let let, const, class, function, var, import
{
let foo;
let foo; // SyntaxError: Identifier 'foo' has already been declared
}
let catch let catch
function foo(a) {
let a = 1; // SyntaxError: Identifier 'a' has already been declared
}
try {
} catch (e) {
let e; // SyntaxError: Identifier 'e' has already been declared
}
Firefox > > REPL let 2 Firefox bug 1580891Chrome REPL let
switch 1
let x = 1;
switch (x) {
case 0:
let foo;
break;
case 1:
let foo; // SyntaxError: Identifier 'foo' has already been declared
break;
}
case
let x = 1;
switch (x) {
case 0: {
let foo;
break;
}
case 1: {
let foo;
break;
}
}
let let var var
function varTest() {
var x = 1;
{
var x = 2; // !
console.log(x); // 2
}
console.log(x); // 2
}
function letTest() {
let x = 1;
{
let x = 2; //
console.log(x); // 2
}
console.log(x); // 1
}
let var
var x = "global";
let y = "global";
console.log(this.x); // "global"
console.log(this.y); // undefined
foo
function test() {
var foo = 33;
if (foo) {
let foo = foo + 55; // ReferenceError
}
}
test();
var foo if if foo let foo foo + 55 ReferenceError let foo
let n of n.a for...of n.a (let n) 'n' a
function go(n) {
// n
console.log(n); // { a: [1, 2, 3] }
for (let n of n.a) {
// ^ ReferenceError
console.log(n);
}
}
go({ a: [1, 2, 3] });
let var
var a = 1;
var b = 2;
{
var a = 11; //
let b = 22; // if
console.log(a); // 11
console.log(b); // 22
}
console.log(a); // 11
console.log(b); // 2
var let var SyntaxError
let x = 1;
{
var x = 2; // SyntaxError
}
=
const result = /(a+)(b+)(c+)/.exec("aaabcc");
let [, a, b, c] = result;
console.log(a, b, c); // "aaa" "b" "cc"
| ECMAScript 2027 LanguageSpecification # sec-let-and-const-declarations |
varconstlet and const - hacks.mozilla.org (2015)let and const in Firefox 44 - blog.mozilla.org (2015)let and var? - Stack Overflow| Web Proxy Viewer | New URL | Original Page |