let

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Temporal Dead Zone and errors with let

Redeclaring the same variable within the same function or block scope raises a SyntaxError.

if (x) {
let foo;
let foo; // SyntaxError thrown.
}

In ECMAScript 2015, let bindings are not subject to Variable Hoisting, which means that let declarations do not move to the top of the current execution context. Referencing the variable in the block before the initialization results in a ReferenceError (contrary to a variable declared with var, which will just have the undefined value). The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.

let 於 for 迴圈的宣告範圍

You can use the let keyword to bind variables locally in the scope of for loops. This is different from the var keyword in the head of a for loop, which makes the variables visible in the whole function containing the loop.

var i=0;
for ( let i=i ; i < 10 ; i++ ) {
console.log(i);
}

However, it's important to point out that a block nested inside a case clause will create a new block scoped lexical environment, which will not produce the redeclaration errors shown above.

Another example of temporal dead zone combined with lexical scoping

Due to lexical scoping, the identifier "foo" inside the expression (foo + 55) evaluates to the if block's foo, and not the overlying variable foo with the value of 33.
In that very line, the if block's "foo" has already been created in the lexical environment, but has not yet reached (and terminated) its initialization (which is part of the statement itself): it's still in the temporal dead zone.

This phenomenon may confuse you in a situation like the following. The instruction let n of n.a is already inside the private scope of the for loop's block, hence the identifier "n.a" is resolved to the property 'a' of the 'n' object located in the first part of the instruction itself ("let n"), which is still in the temporal dead zone since its declaration statement has not been reached and terminated.

1. Prior to Firefox 44, let is only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version) and has different semantics (e.g. no temporal dead zone).

2. Prior to Firefox 46, a TypeError is thrown on redeclaration instead of a SyntaxError.

3. Firefox 54 adds support of let in workers.

Desktop

Mobile

Server

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Android webview

Chrome for Android

Edge Mobile

Firefox for Android

Opera for Android

iOS Safari

Samsung Internet

Node.js

Basic support

Full support
41

Full support
12

Full support
44

Notes

Full support
44

Notes

Notes Prior to Firefox 44, let is only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version) and has different semantics (e.g. no temporal dead zone).

Notes Prior to Firefox 46, a TypeError is thrown on redeclaration instead of a SyntaxError.

Notes Firefox 54 adds support of let in workers.

Full support
11

Full support
17

Full support
10

Full support
41

Full support
41

Full support
12

Full support
44

Notes

Full support
44

Notes

Notes Prior to Firefox 44, let is only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version) and has different semantics (e.g. no temporal dead zone).

Notes Prior to Firefox 46, a TypeError is thrown on redeclaration instead of a SyntaxError.