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 793699 of void operator

  • Revision slug: Web/JavaScript/Reference/Operators/void
  • Revision title: void operator
  • Revision id: 793699
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Better links.

Revision Content

{{jsSidebar("Operators")}}

The void operator evaluates the given expression and then returns {{jsxref("Global_Objects/undefined", "undefined")}}.

Syntax

void expression

Description

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to {{jsxref("Global_Objects/undefined", "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 {{jsxref("Global_Objects/undefined", "undefined")}} can be used instead (assuming it has not been assigned to a non-default value).

Immediately Invoked Function Expressions

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

void function iife() {
    var bar = function () {};
    var baz = function () {};
    var foo = function () {
        bar();
        baz();
     };
    var biz = function () {};

    foo();
    biz();
}();

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 {{jsxref("Global_Objects/undefined", "undefined")}}. The void operator can be used to return {{jsxref("Global_Objects/undefined", "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>{{jsSidebar("Operators")}}</div>

<p>The <strong><code>void</code> operator</strong> evaluates the given <code><em>expression</em></code> and then returns {{jsxref("Global_Objects/undefined", "undefined")}}.</p>

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

<pre class="syntaxbox">
void <em>expression</em></pre>

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

<p>This operator allows inserting expressions that produce side effects into places where an expression that evaluates to {{jsxref("Global_Objects/undefined", "undefined")}} 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 {{jsxref("Global_Objects/undefined", "undefined")}} can be used instead (assuming it has not been assigned to a non-default value).</p>

<h2 id="Immediately_Invoked_Function_Expressions">Immediately Invoked Function Expressions</h2>

<p>When using an <a href="/en-US/docs/Glossary/IIFE">immediately-invoked function expression</a>, <code>void</code> can be used to force the <code>function</code> keyword to be treated as an expression instead of a declaration.</p>

<pre class="brush: js">
void function iife() {
    var bar = function () {};
    var baz = function () {};
    var foo = function () {
        bar();
        baz();
     };
    var biz = function () {};

    foo();
    biz();
}();
</pre>

<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 {{jsxref("Global_Objects/undefined", "undefined")}}. The <code>void</code> operator can be used to return {{jsxref("Global_Objects/undefined", "undefined")}}. 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">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