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 784271 of empty

  • Revision slug: Web/JavaScript/Reference/Statements/Empty
  • Revision title: empty
  • Revision id: 784271
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment lint

Revision Content

{{jsSidebar("Statements")}}

An empty statement is used to provide no statement, although the JavaScript syntax would expect one.

Syntax

;

Description

The empty statement is a semicolon (;) indicating that no statement will be executed, even if JavaScript syntax requires one. The opposite behavior, where you want multiple statements, but JavaScript only allows a single one, is possible using a block statement; it combines several statements into a single one.

Examples

The empty statement is sometimes used with loop statements. See the following example with an empty loop body:

var arr = [1, 2, 3];

// Assign all array values to 0
for (i = 0; i < arr.length; arr[i++] = 0) /* empty statement */ ;

console.log(arr)
// [0, 0, 0]

Note: It is a good idea to comment the intentional use of the empty statement, as it is not really obvious to distinguish between a normal semicolon. In the following example the usage is probably not intentional:

if (condition);       // Caution, this "if" does nothing!
   killTheUniverse()  // So this gets always executed!!!

Another Example: An if...else statement without curly braces ({}). If three is true, nothing will happen, four does not matter, and also the launchRocket() function in the else case will not be executed.

if (one)
  doOne();
else if (two)
  doTwo();
else if (three)
  ; // nothing here
else if (four)
  doFour();
else
  launchRocket();

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
{{SpecName('ES5.1', '#sec-12.3', 'Empty statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-empty-statement', 'Empty statement')}} {{Spec2('ES6')}}  

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/block", "Block statement")}}

Revision Source

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

<p>An <strong>empty statement</strong> is used to provide no statement, although the JavaScript syntax would expect one.</p>

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

<pre class="syntaxbox">
;
</pre>

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

<p>The empty statement is a semicolon (;) indicating that no statement will be executed, even if JavaScript syntax requires one. The opposite behavior, where you want multiple statements, but JavaScript only allows a single one, is possible using a<a href="/en-US/docs/Web/JavaScript/Reference/Statements/block"> block statement</a>; it combines several statements into a single one.</p>

<h2 id="Examples">Examples</h2>

<p>The empty statement is sometimes used with loop statements. See the following example with an empty loop body:</p>

<pre class="brush: js">
var arr = [1, 2, 3];

// Assign all array values to 0
for (i = 0; i &lt; arr.length; arr[i++] = 0) /* empty statement */ ;

console.log(arr)
// [0, 0, 0]
</pre>

<p><strong>Note:</strong> It is a good idea to comment the intentional use of the empty statement, as it is not really obvious to distinguish between a normal semicolon. In the following example the usage is probably not intentional:</p>

<pre class="brush: js">
if (condition);       // Caution, this "if" does nothing!
   killTheUniverse()  // So this gets always executed!!!
</pre>

<p>Another Example: An <a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else"><code>if...else</code></a> statement without curly braces (<code>{}</code>). If <code>three</code> is <code>true</code>, nothing will happen, <code>four</code> does not matter, and also the <code>launchRocket()</code> function in the <code>else</code> case will not be executed.</p>

<pre class="brush: js">
if (one)
  doOne();
else if (two)
  doTwo();
else if (three)
  ; // nothing here
else if (four)
  doFour();
else
  launchRocket();</pre>

<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>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.3', 'Empty statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-empty-statement', 'Empty statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</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/block", "Block statement")}}</li>
</ul>
Revert to this revision