void operator

  • Revision slug: JavaScript/Reference/Operators/void
  • Revision title: void
  • Revision id: 236627
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment 16 words added, 5 words removed

Revision Content

Syntax

void expression

Summary

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

Operator
Implemented in: JavaScript 1.1
ECMA Version: ECMA-262

Uses

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).

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.

{{ languages({ "es": "es/Referencia_de_JavaScript_1.5/Operadores/Especiales/void", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_sp\u00e9ciaux/L\'op\u00e9rateur_void", "ja": "ja/JavaScript/Reference/Operators/void", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_void"}) }}

Revision Source

<h2>Syntax</h2>
<p><code>void <em>expression</em></code></p>
<h2>Summary</h2>
<p>The <code>void</code> operator evaluates the given <code><em>expression</em></code> and then returns <code>undefined</code>.</p>
<table class="standard-table"> <tbody> <tr> <td class="header" colspan="2">Operator</td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.1</td> </tr> <tr> <td>ECMA Version:</td> <td>ECMA-262</td> </tr> </tbody>
</table>
<h2>Uses</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/JavaScript/Reference/Global_Objects/undefined" title="en/JavaScript/Reference/Global_Objects/undefined">undefined</a> can be used instead (assuming it has not been assigned to a non-default value).</p>
<h2>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>&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>
<div class="noinclude">{{ languages({
"es": "es/Referencia_de_JavaScript_1.5/Operadores/Especiales/void",
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_sp\u00e9ciaux/L\'op\u00e9rateur_void",
"ja": "ja/JavaScript/Reference/Operators/void",
"pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_void"}) }}</div>
Revert to this revision