mozilla

Revision 33139 of let

  • Revision slug: JavaScript/Reference/Statements/let
  • Revision title: let
  • Revision id: 33139
  • Created:
  • Creator: Markus Prokott
  • Is current revision? No
  • Comment Another typo fixed.; 1 words added

Revision Content

{{ Non-standard_header() }} {{ js_minversion_header("1.7") }}

Summary

Declares a local variable, optionally initializing it to a value.

Syntax

let definition:

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

let expression:

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

let statement:

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

Parameters

Parameter Description
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.
expression Any legal expression.
statement Any legal statement.

Description

let allows you to declare variables, limiting its 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.

Examples

A let expression limits the scope of the declared variable to only that expression.

var a = 5;
let(a = 6) alert(a); // 6
alert(a); // 5

 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 = 5;
var b = 10;
if (a === 5) {
  let a = 4; // The scope is inside the if-block
  var b = 1; // The scope is inside the function

  console.log(a);  // 4
  console.log(b);  // 1
} 
console.log(a); // 5
console.log(b); // 1  

You can use the let keyword to bind variables locally in the scope of loops instead of using a global variable (defined using var) for that.

for (let i = 0; i<10; i++) {
  alert(i); // 1, 2, 3, 4 ... 9
}

alert(i); // i is not defined

See also

{{ languages( { "ja": "ja/JavaScript/Reference/Statements/let"} ) }}

Revision Source

<p>{{ Non-standard_header() }} {{ js_minversion_header("1.7") }}</p>
<h2>Summary</h2>
<p>Declares a local variable, optionally initializing it to a value.</p>
<h2>Syntax</h2>
<p><code>let</code> definition:</p>
<pre class="brush: js">let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];
</pre>
<p><code>let</code> expression:</p>
<pre class="brush: js">let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) expression;
</pre>
<p><code>let</code> statement:</p>
<pre class="brush: js">let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) statement;
</pre>
<h2>Parameters</h2>
<table class="standard-table" style=""> <thead> <tr> <th scope="col">Parameter</th> <th scope="col">Description</th> </tr> </thead> <tbody> <tr> <td><code>var1</code>, <code>var2</code>, …, <code>varN</code></td> <td>Variable name. It can be any legal identifier.</td> </tr> <tr> <td><code>value1</code>, <code>value2</code>, …, <code>valueN</code></td> <td>Initial value of the variable. It can be any legal expression.</td> </tr> <tr> <td><code>expression</code></td> <td>Any legal <a href="/en/JavaScript/Guide/Expressions_and_Operators#Expressions" title="en/JavaScript/Guide/Expressions and Operators#Expressions">expression</a>.</td> </tr> <tr> <td><code>statement</code></td> <td>Any legal <a href="/en/JavaScript/Reference/Statements" title="en/JavaScript/Reference/Statements">statement</a>.</td> </tr> </tbody>
</table>
<h2>Description</h2>
<p><code>let</code> allows you to declare variables, limiting its scope to the block, statement, or expression on which it is used. This is unlike the <a href="/en/JavaScript/Reference/Statements/var" title="en/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>
<h2>Examples</h2>
<p>A <em>let expression</em> limits the scope of the declared variable to only that expression.</p>
<pre class="brush: js">var a = 5;
let(a = 6) alert(a); // 6
alert(a); // 5
</pre>
<p> 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 = 5;
var b = 10;
if (a === 5) {
  let a = 4; // The scope is inside the if-block
  var b = 1; // The scope is inside the function

  console.log(a);  // 4
  console.log(b);  // 1
} 
console.log(a); // 5
console.log(b); // 1  
</pre>
<p>You can use the <em>let</em> keyword to bind variables locally in the scope of loops instead of using a global variable (defined using <em>var</em>) for that.</p>
<pre class="brush: js">for (let i = 0; i&lt;10; i++) {
  alert(i); // 1, 2, 3, 4 ... 9
}

alert(i); // i is not defined

</pre>
<h2>See also</h2>
<ul> <li><a href="/en/JavaScript/Reference/Statements/var" title="en/JavaScript/Reference/Statements/var"><code>var</code></a></li> <li><a href="/en/JavaScript/Reference/Statements/const" title="en/JavaScript/Reference/Statements/const"><code>const</code></a></li> <li><a href="/en/JavaScript/New_in_JavaScript/1.7#Block_scope_with_let_(Merge_into_let_Statement)" title="en/JavaScript/New in JavaScript/1.7#Block scope with let (Merge into let Statement)">New in JavaScript 1.7</a></li>
</ul>
<p>{{ languages( { "ja": "ja/JavaScript/Reference/Statements/let"} ) }}</p>
Revert to this revision