mozilla

Revision 595011 of void operator

  • Revision slug: Web/JavaScript/Reference/Operators/void
  • Revision title: void operator
  • Revision id: 595011
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSOperatorsQLAlpha()}}

Summary

The void operator evaluates the given expression and then returns undefined.

Syntax

void expression

Description

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.

The void operator is often used merely to obtain the undefined primitive value, usually using "void(0)" (which is equivalent to "void 0"). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

When wrapping an entire script in an immediately-invoked function expression, void can be used to force the function keyword to be treated as an expression instead of a declaration.

JavaScript URIs

When a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined. For example:

<a href="javascript:void(0);">
  Click here to do nothing
</a>

<a href="javascript:void(document.body.style.backgroundColor='green');">
  Click here for green background
</a>

Note, however, that the javascript: pseudo protocol is discouraged over other alternatives, such as unobtrusive event handlers.

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.1
{{SpecName('ES5.1', '#sec-11.4.2', 'The void Operator')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-void-operator', 'The void Operator')}} {{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

Revision Source

<div>
 {{JSOperatorsQLAlpha()}}</div>
<h2 id="Summary">Summary</h2>
<p>The <code>void</code> operator evaluates the given <code><em>expression</em></code> and then returns <code>undefined</code>.</p>
<h2 id="Syntax">Syntax</h2>
<pre>
<code>void <em>expression</em></code></pre>
<h2 id="Uses">Description</h2>
<p>This operator allows inserting expressions that produce side effects into places where an expression that evaluates to <code>undefined</code> is desired.</p>
<p>The <code>void</code> operator is often used merely to obtain the <code>undefined</code> primitive value, usually using "<code>void(0)</code>" (which is equivalent to "<code>void 0</code>"). In these cases, the global variable <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined" title="/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined"><code>undefined</code></a> can be used instead (assuming it has not been assigned to a non-default value).</p>
<p>When wrapping an entire script in an immediately-invoked function expression, <code>void</code> can be used to force the <code>function</code> keyword to be treated as an expression instead of a declaration.</p>
<h2 id="JavaScript_URIs">JavaScript URIs</h2>
<p>When a browser follows a <code>javascript:</code> URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is <code>undefined</code>. The <code>void</code> operator can be used to return <code>undefined</code>. For example:</p>
<pre class="brush: html">
&lt;a href="javascript:void(0);"&gt;
  Click here to do nothing
&lt;/a&gt;

&lt;a href="javascript:void(document.body.style.backgroundColor='green');"&gt;
  Click here for green background
&lt;/a&gt;
</pre>
<p>Note, however, that the <code>javascript:</code> pseudo protocol is discouraged over other alternatives, such as unobtrusive event handlers.</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>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.1</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-11.4.2', 'The void Operator')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-void-operator', 'The void Operator')}}</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" name="See_also">See also</h2>
<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined">undefined</a></code></li>
</ul>
Revert to this revision