Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1072382 of let

  • Revision slug: Web/JavaScript/Reference/Statements/let
  • Revision title: let
  • Revision id: 1072382
  • Created:
  • Creator: vitaly-zdanevich
  • Is current revision? No
  • Comment removed incorrect example: I can console.log(i) of let after loop without error at Firefox 47

Revision Content

{{jsSidebar("Statements")}}

The let statement declares a block scope local variable, optionally initializing it to a value.

Syntax

let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];

Parameters

var1, var2, …, varN
Variable name. It can be any legal identifier.
value1, value2, …, valueN
Initial value of the variable. It can be any legal expression.

Description

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Scoping rules

Variables declared by let have as their scope the block in which they are defined, as well as in any contained sub-blocks . In this way, let works very much like var. The main difference is that the scope of a var variable is the entire enclosing function:

function varTest() {
  var x = 1;
  if (true) {
    var x = 2;  // same variable!
    console.log(x);  // 2
  }
  console.log(x);  // 2
}

function letTest() {
  let x = 1;
  if (true) {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}

Cleaner code in inner functions

let sometimes makes the code cleaner when inner functions are used.

var list = document.getElementById("list");

for (let i = 1; i <= 5; i++) {
  let item = document.createElement("li");
  item.appendChild(document.createTextNode("Item " + i));

  item.onclick = function (ev) {
    console.log("Item " + i + " is clicked.");
  };
  list.appendChild(item);
}

The example above works as intended because the five instances of the (anonymous) inner function refer to five different instances of the variable i. Note that it does not work as intended if you replace let with var, since all of the inner functions would then return the same final value of i: 6. Also, we can keep the scope around the loop cleaner by moving the code that creates the new elements into the scope of each loop.

At the top level of programs and functions, let, unlike var, does not create a property on the global object. For example:

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

Temporal dead zone and errors with let

Redeclaring the same variable within the same function or block scope raises a {{jsxref("SyntaxError")}}.

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

In ECMAScript 2015, let will hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.

function do_something() {
  console.log(foo); // ReferenceError
  let foo = 2;
}

You may encounter errors in switch statements because there is only one underlying block.

switch (x) {
  case 0:
    let foo;
    break;
    
  case 1:
    let foo; // SyntaxError for redeclaration.
    break;
}

 

Another example

When used inside a block, let limits the variable's scope to that block. Note the difference between var whose scope is inside the function where it is declared.

var a = 1;
var b = 2;

if (a === 1) {
  var a = 11; // the scope is global
  let b = 22; // the scope is inside the if-block

  console.log(a);  // 11
  console.log(b);  // 22
} 

console.log(a); // 11
console.log(b); // 2

 

Non-standard let extensions

let blocks

let blocks support has been dropped in Gecko 44 ({{bug(1167029)}}).

The let block provides a way to associate values with variables within the scope of a block, without affecting the values of like-named variables outside the block.

Syntax

let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) block;

Description

The let block provides local scoping for variables. It works by binding zero or more variables in the lexical scope of a single block of code; otherwise, it is exactly the same as a block statement. Note in particular that the scope of a variable declared inside a let block using var is still the same as if it had been declared outside the let block; such variables still have function scoping. When using the let block syntax, the parentheses following let are required. Failure to include them will result in a syntax error.

Example

var x = 5;
var y = 0;

let (x = x+10, y = 12) {
  console.log(x+y); // 27
}

console.log(x + y); // 5

The rules for the code block are the same as for any other code block in JavaScript. It may have its own local variables established using the let declarations.

Scoping rules

The scope of variables defined using let is the let block itself, as well as any inner blocks contained inside it, unless those blocks define variables by the same names.

let expressions

let expression support has been dropped in Gecko 41 ({{bug(1023609)}}).

The let expression lets you establish variables scoped only to a single expression.

Syntax

let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) expression;

Example

You can use let to establish variables that are scoped only to a single expression:

var a = 5;
let(a = 6) console.log(a); // 6
console.log(a); // 5

Scoping rules

Given a let expression:

let (decls) expr

There is an implicit block created around expr.

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-let-and-const-declarations', 'Let and Const Declarations')}} {{Spec2('ES6')}} Initial definition. Does not specify let expressions or let blocks.
{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatChrome(41.0)}} 12 {{ CompatGeckoDesktop(44) }} 11 17 {{CompatUnknown}}
Temporal dead zone {{CompatUnknown}} 12 {{ CompatGeckoDesktop(35) }} 11 {{CompatUnknown}} {{CompatUnknown}}
let expression {{non-standard_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
let block {{non-standard_inline}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Allowed in {{Glossary("sloppy mode")}} {{CompatChrome(49.0)}} {{CompatUnknown}} {{CompatGeckoDesktop(44)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatUnknown}} {{CompatChrome(41.0)}} {{ CompatGeckoMobile(44) }} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(41.0)}}
Temporal dead zone {{CompatUnknown}} {{CompatUnknown}} {{ CompatGeckoMobile(35) }} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
let expression {{non-standard_inline}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
let block {{non-standard_inline}} {{CompatNo}} {{CompatUnknown}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Allowed in {{Glossary("sloppy mode")}} {{CompatNo}} {{CompatChrome(49.0)}} {{CompatGeckoDesktop(44)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(49.0)}}

Firefox-specific notes

  • Prior to SpiderMonkey 46 {{geckoRelease(46)}}, a {{jsxref("TypeError")}} was thrown on redeclaration instead of a {{jsxref("SyntaxError")}} ({{bug(1198833)}}, {{bug(1275240)}}).
  • Prior to SpiderMonkey 44 {{geckoRelease(44)}}, let was only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version) and had different semantics.
  • Support in {{domxref("Worker")}} code is hidden behind the dom.workers.latestJSVersion flag ({{bug(487070)}}). With version free let, this flag is going to be removed in the future ({{bug(1219523)}}).
  • ES6 compliance for let in SpIderMonkey is tracked in {{bug(950547)}}

See also

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The <strong><code>let</code></strong> statement declares a block scope local variable, optionally initializing it to a value.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>var1</code>, <code>var2</code>, …, <code>varN</code></dt>
 <dd>Variable name. It can be any legal identifier.</dd>
 <dt><code>value1</code>, <code>value2</code>, …, <code>valueN</code></dt>
 <dd>Initial value of the variable. It can be any legal expression.</dd>
</dl>

<h2 id="Description">Description</h2>

<p><code>let</code> allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the <a href="/en-US/docs/JavaScript/Reference/Statements/var" title="JavaScript/Reference/Statements/var"><code>var</code></a> keyword, which defines a variable globally, or locally to an entire function regardless of block scope.</p>

<h4 id="Scoping_rules_2">Scoping rules</h4>

<p>Variables declared by <code>let</code> have as their scope the block in which they are defined, as well as in any contained sub-blocks . In this way, <code>let</code> works very much like <code>var</code>. The main difference is that the scope of a <code>var</code> variable is the entire enclosing function:</p>

<pre class="brush:js">
function varTest() {
  var x = 1;
  if (true) {
    var x = 2;  // same variable!
    console.log(x);  // 2
  }
  console.log(x);  // 2
}

function letTest() {
  let x = 1;
  if (true) {
    let x = 2;  // different variable
    console.log(x);  // 2
  }
  console.log(x);  // 1
}
</pre>

<h3 id="Cleaner_code_in_inner_functions">Cleaner code in inner functions</h3>

<p><code>let</code> sometimes makes the code cleaner when inner functions are used.</p>

<pre class="brush:js">
var list = document.getElementById("list");

for (let i = 1; i &lt;= 5; i++) {
  let item = document.createElement("li");
  item.appendChild(document.createTextNode("Item " + i));

  item.onclick = function (ev) {
    console.log("Item " + i + " is clicked.");
  };
  list.appendChild(item);
}
</pre>

<p>The example above works as intended because the five instances of the (anonymous) inner function refer to five different instances of the variable <code>i</code>. Note that it does not work as intended if you replace <code>let</code> with <code>var</code>, since all of the inner functions would then return the same final value of <code>i</code>: 6. Also, we can keep the scope around the loop cleaner by moving the code that creates the new elements into the scope of each loop.</p>

<p id="Scoping_rules">At the top level of programs and functions, <code>let</code>, unlike <code>var</code>, does not create a property on the global object. For example:</p>

<pre class="brush:js">
var x = 'global';
let y = 'global';
console.log(this.x); // <code>"global"</code>
console.log(this.y); // <code>undefined</code>
</pre>

<p>Temporal dead zone and errors with <code>let</code></p>

<p>Redeclaring the same variable within the same function or block scope raises a {{jsxref("SyntaxError")}}.</p>

<pre class="brush: js">
if (x) {
  let foo;
  let foo; // SyntaxError thrown.
}</pre>

<p>In ECMAScript 2015, <code>let</code>&nbsp;<strong>will hoist</strong>&nbsp;the variable to the top of the block. However, referencing&nbsp;the&nbsp;variable in the&nbsp;block before the variable declaration&nbsp;results in a <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/ReferenceError" title="TypeError">ReferenceError</a>.</code>&nbsp;The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.</p>

<pre class="brush: js">
function do_something() {
  console.log(foo); // ReferenceError
  let foo = 2;
}</pre>

<p>You may encounter errors in <a href="/en-US/docs/JavaScript/Reference/Statements/switch" title="switch"><code>switch</code></a> statements because there is only one underlying block.</p>

<pre class="brush: js">
switch (x) {
  case 0:
    let foo;
    break;
    
  case 1:
    let foo; // SyntaxError for redeclaration.
    break;
}</pre>

<h3 id="sect1">&nbsp;</h3>

<h2 id="Another_example">Another example</h2>

<p>When used inside a block, <em>let</em> limits the variable's scope to that block. Note the difference between <em>var </em>whose scope is inside the function where it is declared.</p>

<pre class="brush: js">
var a = 1;
var b = 2;

if (a === 1) {
  var a = 11; // the scope is global
  let b = 22; // the scope is inside the if-block

  console.log(a);  // 11
  console.log(b);  // 22
} 

console.log(a); // 11
console.log(b); // 2</pre>

<h3 id="let_in_loops">&nbsp;</h3>

<h2 id="Non-standard_let_extensions">Non-standard <code>let</code> extensions</h2>

<h3 id="let_blocks"><code>let</code> blocks</h3>

<div class="warning">
<p><code>let</code> blocks support has been dropped in Gecko 44 ({{bug(1167029)}}).</p>
</div>

<p>The <strong><code>let</code> block</strong> provides a way to associate values with variables within the scope of a block, without affecting the values of like-named variables outside the block.</p>

<h4 id="Syntax_2">Syntax</h4>

<pre class="syntaxbox">
let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) block;</pre>

<h4 id="Description_2">Description</h4>

<p>The <code>let</code> block provides local scoping for variables. It works by binding zero or more variables in the lexical scope of a single block of code; otherwise, it is exactly the same as a block statement. Note in particular that the scope of a variable declared inside a <code>let</code> block using <code>var</code> is still the same as if it had been declared outside the <code>let</code> block; such variables still have function scoping. When using the <code>let</code> block syntax, the parentheses following <code>let</code> are required. Failure to include them will result in a syntax error.</p>

<h4 id="Example">Example</h4>

<pre class="brush:js">
var x = 5;
var y = 0;

let (x = x+10, y = 12) {
  console.log(x+y); // 27
}

console.log(x + y); // 5
</pre>

<p>The rules for the code block are the same as for any other code block in JavaScript. It may have its own local variables established using the <code>let</code> declarations.</p>

<h4 id="Scoping_rules_3">Scoping rules</h4>

<p>The scope of variables defined using <code>let</code> is the <code>let</code> block itself, as well as any inner blocks contained inside it, unless those blocks define variables by the same names.</p>

<h3 id="let_expressions"><code>let</code> expressions</h3>

<div class="warning">
<p><code>let</code> expression support has been dropped in Gecko 41 ({{bug(1023609)}}).</p>
</div>

<p>The <strong><code>let</code> expression</strong> lets you establish variables scoped only to a single expression.</p>

<h4 id="Syntax_3">Syntax</h4>

<pre class="syntaxbox">
let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) expression;</pre>

<h4 id="Example_2">Example</h4>

<p>You can use <code>let</code> to establish variables that are scoped only to a single expression:</p>

<pre class="brush: js">
var a = 5;
let(a = 6) console.log(a); // 6
console.log(a); // 5</pre>

<h4 id="Scoping_rules_4">Scoping rules</h4>

<p>Given a <code>let</code> expression:</p>

<pre class="eval">
let (<var>decls</var>) <var>expr</var>
</pre>

<p>There is an implicit block created around <var>expr</var>.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition. Does not specify let expressions or let blocks.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatChrome(41.0)}}</td>
   <td>12</td>
   <td>{{ CompatGeckoDesktop(44) }}</td>
   <td>11</td>
   <td>17</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td>Temporal dead zone</td>
   <td>{{CompatUnknown}}</td>
   <td>12</td>
   <td>{{ CompatGeckoDesktop(35) }}</td>
   <td>11</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>let</code> expression {{non-standard_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>let</code> block {{non-standard_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Allowed in {{Glossary("sloppy mode")}}</td>
   <td>{{CompatChrome(49.0)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(44)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(41.0)}}</td>
   <td>{{ CompatGeckoMobile(44) }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(41.0)}}</td>
  </tr>
  <tr>
   <td>Temporal dead zone</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{ CompatGeckoMobile(35) }}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
  <tr>
   <td><code>let</code> expression {{non-standard_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td><code>let</code> block {{non-standard_inline}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Allowed in {{Glossary("sloppy mode")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatChrome(49.0)}}</td>
   <td>{{CompatGeckoDesktop(44)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatChrome(49.0)}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<ul>
 <li>Prior to SpiderMonkey 46 {{geckoRelease(46)}}, a {{jsxref("TypeError")}} was thrown on redeclaration instead of a {{jsxref("SyntaxError")}} ({{bug(1198833)}}, {{bug(1275240)}}).</li>
 <li>Prior to SpiderMonkey 44 {{geckoRelease(44)}}, <code>let</code> was only available to code blocks in HTML&nbsp;wrapped in a <code>&lt;script type="application/javascript;version=1.7"&gt;</code> block (or higher version) and had different semantics.</li>
 <li>Support in {{domxref("Worker")}} code is hidden behind the <code>dom.workers.latestJSVersion</code> flag ({{bug(487070)}}). With version free <code>let</code>, this flag is going to be removed in the future ({{bug(1219523)}}).</li>
 <li>ES6 compliance for <code>let</code> in SpIderMonkey is tracked in {{bug(950547)}}</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var"><code>var</code></a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const"><code>const</code></a></li>
 <li><a href="https://hacks.mozilla.org/2015/07/es6-in-depth-let-and-const/">ES6 In Depth: <code>let</code> and <code>const</code></a></li>
 <li><a href="https://blog.mozilla.org/addons/2015/10/14/breaking-changes-let-const-firefox-nightly-44/">Breaking changes in <code>let</code> and <code>const</code> in Firefox 44.</a></li>
</ul>
Revert to this revision