mozilla

Revision 64147 of void operator

  • Revision slug: JavaScript/Reference/Operators/void
  • Revision title: void
  • Revision id: 64147
  • Created:
  • Creator: HeroreV
  • Is current revision? No
  • Comment void(0) evaluates to 0? LOL no.

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 javascript: URIs are now often discouraged over other alternatives, such as events.

{{ 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", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_void" } ) }}

Revision Source

<p>
</p>
<h3 name="Syntax"> Syntax </h3>
<p><code>void <i>expression</i></code>
</p>
<h3 name="Summary"> Summary </h3>
<p>The <code>void</code> operator evaluates the given <code><i>expression</i></code> and then returns <code>undefined</code>.
</p>
<table class="fullwidth-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>
<h3 name="Uses"> Uses </h3>
<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/Core_JavaScript_1.5_Reference/Global_Properties/undefined">undefined</a> can be used instead (assuming it has not been assigned to a non-default value).
</p>
<h3 name="JavaScript_URIs"> JavaScript URIs </h3>
<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 <code>javascript:</code> URIs are now often discouraged over other alternatives, such as events.
</p>
<div class="noinclude">
</div>
{{ 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", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_void" } ) }}
Revert to this revision