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 845179 of block

  • Revision slug: Web/JavaScript/Reference/Statements/block
  • Revision title: block
  • Revision id: 845179
  • Created:
  • Creator: Sebastianz
  • Is current revision? No
  • Comment Replaced static text for specification by macros and added ES3 spec

Revision Content

{{jsSidebar("Statements")}}

A block statement (or compound statement in other languages) is used to group zero or more statements. The block is delimited by a pair of curly brackets.

Syntax

{
  statement_1;
  statement_2;
  ...
  statement_n;
}
statement_1, statement_2, statement_n
Statements grouped within the block statement.

Description

This statement is commonly used with control flow statements (e.g. {{jsxref("Statements/if...else", "if...else")}}, {{jsxref("Statements/for", "for")}}, {{jsxref("Statements/while", "while")}}). For example:

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

Note that the block statement does not end with a semicolon.

The block statement is often called compound statement in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript. The opposite behavior is possible using an empty statement, where you provide no statement, although one is required.

No block scope

Important: Variables declared with var do 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;
}
console.log(x); // logs 2

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

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-block', 'Block statement')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-12.1', 'Block statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES3', '#sec-12.1', 'Block statement')}} {{Spec2('ES3')}}  
{{SpecName('ES1', '#sec-12.1', 'Block statement')}} {{Spec2('ES1')}} Initial definition. Implemented in JavaScript 1.0.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

  • {{jsxref("Statements/while", "while")}}

Revision Source

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

<p>A<strong> block statement</strong> (or <strong>compound statement</strong> in other languages) is used to group zero or more statements. The block is delimited by a pair of curly brackets.</p>

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

<pre class="syntaxbox">
{
  <em>statement_1</em>;
  <em>statement_2;</em>
  ...
  <em>statement_n;</em>
}
</pre>

<dl>
 <dt><code>statement_1</code>, <code>statement_2</code>, <code>statement_n</code></dt>
 <dd>Statements grouped within the block statement.</dd>
</dl>

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

<p>This statement is commonly used with control flow statements (e.g. {{jsxref("Statements/if...else", "if...else")}}, {{jsxref("Statements/for", "for")}}, {{jsxref("Statements/while", "while")}}). For example:</p>

<pre class="brush: js">
while (x &lt; 10) {
  x++;
}
</pre>

<p>Note that the block statement does not end with a semicolon.</p>

<p>The block statement is often called <strong>compound statement</strong> in other languages. It allows you to use multiple statements where JavaScript expects only one statement. Combining statements into blocks is a common practice in JavaScript. The opposite behavior is possible using an <a href="/en-US/docs/Web/JavaScript/Reference/Statements/Empty">empty statement</a>, where you provide no statement, although one is required.</p>

<h3 id="No_block_scope">No block scope</h3>

<p><strong>Important</strong>: Variables declared with <code>var</code> do <strong>not</strong> 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="brush: js">
var x = 1;
{
  var x = 2;
}
console.log(x); // logs 2
</pre>

<p>This logs 2 because the <code>var x</code> statement within the block is in the same scope as the <code>var x</code> statement before the block. In C or Java, the equivalent code would have outputted 1.</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-block', 'Block statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.1', 'Block statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES3', '#sec-12.1', 'Block statement')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES1', '#sec-12.1', 'Block statement')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.0.</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>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

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

<ul>
 <li>{{jsxref("Statements/while", "while")}}</li>
</ul>
Revert to this revision