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

let - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

let

Baseline

20169

let

let x = 1;

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

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

console.log(x);
// : 1

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

nameN

JavaScript

valueN

undefined

let let

var let

  • let

  • let let

  • let globalThis

  • let

  • let let

    js
    if (true) let a = 1; // SyntaxError: Lexical declaration cannot appear in a single-statement context
    

let var function let

MDN let const let

let =

(TDZ)

letconstclass (TDZ)

TDZ ReferenceError undefined

var undefined let var

js
{
  // TDZ 
  console.log(bar); // "undefined"
  console.log(foo); // ReferenceError: Cannot access 'foo' before initialization
  var bar = 1;
  let foo = 2; // TDZ foo 
}

let TDZ

js
{
  // TDZ 
  const func = () => console.log(letVar); // OK

  // TDZ letVar  `ReferenceError` 

  let letVar = 3; // TDZ letVar 
  func(); // TDZ 
}

typeof ReferenceError

js
{
  typeof i; // ReferenceError: Cannot access 'i' before initialization
  let i = 10;
}

typeof undefined

js
console.log(typeof undeclaredVariable); // "undefined"

: let const 1 HTML 2 <script> 1 2 let const TDZ 1 let const 2

let let, const, class, function, var, import

js
{
  let foo;
  let foo; // SyntaxError: Identifier 'foo' has already been declared
}

let catch let catch

js
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

js
let x = 1;

switch (x) {
  case 0:
    let foo;
    break;
  case 1:
    let foo; // SyntaxError: Identifier 'foo' has already been declared
    break;
}

case

js
let x = 1;

switch (x) {
  case 0: {
    let foo;
    break;
  }
  case 1: {
    let foo;
    break;
  }
}

let let var var

js
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

js
var x = "global";
let y = "global";
console.log(this.x); // "global"
console.log(this.y); // undefined

foo

js
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

js
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

js
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

js
let x = 1;

{
  var x = 2; //  SyntaxError
}

=

js
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


Web Proxy Viewer  |  New URL  |  Original Page