mozilla

Revision 81700 of block

  • Revision slug: JavaScript/Reference/Statements/block
  • Revision title: block
  • Revision id: 81700
  • Created:
  • Creator: Maian
  • Is current revision? No
  • Comment /* Syntax */

Revision Content

Summary

A block statement is used to group zero or more statements. The block is delimited by a pair of curly brackets.

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

Syntax

{
   statement_1
   statement_2
   ...
   statement_n
}

Parameters

statement_1, statement_2, statement_n
Statements grouped within the block statement.

Description

This statement is commonly used with control flow statements (e.g. if, for, while). For example:

while (x < 10) {
   x++;
}

Important: JavaScript does not have block scope. Variables introduced with a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java. For example:

var x = 1;
{
   var x = 2;
}
alert(x); // outputs 2

This outputs 2 because the var x statement within the conditional is in the same scope as the var x statement before the conditional. In C or Java, the equivalent code would have outputted 1.

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>A block statement is used to group zero or more statements. The block is delimited by a pair of curly brackets.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Statement</td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.0, NES 2.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">{
   <i>statement_1</i>
   <i>statement_2</i>
   ...
   <i>statement_n</i>
}
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>statement_1</code>, <code>statement_2</code>, <code>statement_n</code></dt><dd> Statements grouped within the block statement.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>This statement is commonly used with control flow statements (e.g. <code>if</code>, <code>for</code>, <code>while</code>). For example:
</p>
<pre class="eval">while (x &lt; 10) {
   x++;
}
</pre>
<p><b>Important</b>: JavaScript does <b>not</b> have block scope. Variables introduced with a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java. For example:
</p>
<pre class="eval">var x = 1;
{
   var x = 2;
}
alert(x); // outputs 2
</pre>
<p>This outputs 2 because the <code>var x</code> statement within the conditional is in the same scope as the <code>var x</code> statement before the conditional. In C or Java, the equivalent code would have outputted 1.
</p>
Revert to this revision