let

  • リビジョンの URL スラッグ: JavaScript/Reference/Statements/let
  • リビジョンのタイトル: let
  • リビジョンの ID: 338895
  • 作成日:
  • 作成者: ethertank
  • 現行リビジョン いいえ
  • コメント

このリビジョンの内容

{{js_minversion_header("1.7")}}

概要

局所変数を宣言します。任意で値を代入して初期化できます。

構文

let 宣言:

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

let 式:

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

let 文:

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

引数

引数 説明
var1, var2, …, varN 変数の名前。任意の有効な識別子を指定できます。
value1, value2, …, valueN 変数の初期値。任意の有効な式を指定できます。
expression 任意の有効な
statement 任意の有効な

説明

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.

Block scoping

let declared variables are hoisted to the beginning of the enclosing block.

Redeclaration of the same variable in the same block scope raises a TypeError.

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

However, function bodies do not have this limitation!

function do_something() {
  let foo;
  let foo; // This works fine.
}
Warning: ECMAScript 6 drafts (as of April, 2012) make this behavior illegal. That means that future versions of JavaScript will likely be consistent and raise a TypeError. if you do this, so you should avoid this practice!

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; // TypeError for redeclaration.
    break;
}

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

関連情報

このリビジョンのソースコード

<div>{{js_minversion_header("1.7")}}</div>

<h2 id="Summary" name="Summary">概要</h2>


<p>局所変数を宣言します。任意で値を代入して初期化できます。</p>

<h2 id="Syntax" name="Syntax">構文</h2>
<p><code>let</code> 宣言:</p>
<pre class="syntaxbox">
let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];
</pre>


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


<p><code>let</code> 文:</p>
<pre class="syntaxbox">
let (var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]) statement;
</pre>



<h3 id="Parameters" name="Parameters">引数</h3>
<table class="standard-table" style="">
  <thead>
    <tr>
      <th scope="col">引数</th>
      <th scope="col">説明</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>var1</code>, <code>var2</code>, …, <code>varN</code></td>
      <td>変数の名前。任意の有効な識別子を指定できます。</td>
    </tr>
    <tr>
      <td><code>value1</code>, <code>value2</code>, …, <code>valueN</code></td>
      <td>変数の初期値。任意の有効な式を指定できます。</td>
    </tr>
    <tr>
      <td><code>expression</code></td>
      <td>任意の有効な <a href="/ja/docs/JavaScript/Guide/Expressions_and_Operators#Expressions" title="JavaScript/Guide/Expressions and Operators#Expressions">式</a></td>
    </tr>
    <tr>
      <td><code>statement</code></td>
      <td>任意の有効な <a href="/ja/docs/JavaScript/Reference/Statements" title="JavaScript/Reference/Statements">文</a></td>
    </tr>
  </tbody>
</table>



<h2 id="Description" name="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="/ja/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>


<h2 id="Block_scoping" name="Block_scoping">Block scoping</h2>
<p><code>let</code> declared variables are hoisted to the beginning of the enclosing block.</p>
<p>Redeclaration of the same variable in the same block scope raises a <code><a href="/ja/docs/JavaScript/Reference/Global_Objects/TypeError" title="TypeError">TypeError</a></code>.</p>
<pre class="brush: js">
if (x) {
  let foo;
  let foo; // TypeError thrown.
}</pre>
<p>However, function bodies do not have this limitation!</p>
<pre class="brush: js">
function do_something() {
  let foo;
  let foo; // This works fine.
}</pre>
<div class="warning">
  <strong>Warning:</strong> ECMAScript 6 drafts (as of April, 2012) make this behavior illegal. That means that future versions of JavaScript will likely be consistent and raise a <code><a href="/ja/docs/JavaScript/Reference/Global_Objects/TypeError" title="TypeError">TypeError</a></code>. if you do this, so you should avoid this practice!</div>
<p>You may encounter errors in <a href="/ja/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; // TypeError for redeclaration.
    break;
}</pre>


<h2 id="Examples" name="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 id="See_also" name="See_also">関連情報</h2>

<ul>
  <li><a href="/ja/docs/JavaScript/Reference/Statements/var" title="JavaScript/Reference/Statements/var"><code>var</code></a></li>
  <li><a href="/ja/docs/JavaScript/Reference/Statements/const" title="JavaScript/Reference/Statements/const"><code>const</code></a></li>
  <li><a href="/ja/docs/JavaScript/New_in_JavaScript/1.7#Block_scope_with_let" title="JavaScript/New in JavaScript/1.7#Block scope with let">JavaScript 1.7 の新機能</a></li>
</ul>
Revert to this revision