Apply your JS skills to key Mozilla projects as an MDN Fellow! http://mzl.la/MDNFellowship

mozilla

Revision 33127 of let

  • Revision slug: JavaScript/Reference/Statements/let
  • Revision title: let
  • Revision id: 33127
  • Created:
  • Creator: tehsis
  • Is current revision? No
  • Comment Indent fixed; one or more formatting changes

Revision Content

{{ Non-standard_header() }}{{ stub() }} {{ 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 to declare variables limiting its scope to the block, statement or expression on which is used.

Examples

 Using inside a block, let limits the variable's scope to that block. Note the difference between var which its scope is inside the function where 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

     alert(a);  // 4
     alert(b);  // 1
    } 
  alert(a); // 5
  alert(b); // 1  

 

See also

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

Revision Source

{{ Non-standard_header() }}{{ stub() }} {{ js_minversion_header("1.7") }}
<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>Let allows to declare variables limiting its scope to the block, statement or expression on which is used.</p><h2>Examples</h2>
<p> Using inside a block, <em>let</em> limits the variable's scope to that block. Note the difference between <em>var </em>which its scope is inside the function where 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

     alert(a);  // 4
     alert(b);  // 1
    } 
  alert(a); // 5
  alert(b); // 1  
</pre>
<p> </p>
<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