Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>What does <code>void 0</code> mean?</h2> <p><a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/void_Operator" rel="noreferrer"><code>void</code><sup>[MDN]</sup></a> is a prefix keyword that takes one argument and always returns <code>undefined</code>.</p> <p><strong>Examples</strong></p> <pre><code>void 0 void (0) void "hello" void (new Date()) //all will return undefined </code></pre> <h2>What's the point of that?</h2> <p>It seems pretty useless, doesn't it? If it always returns <code>undefined</code>, what's wrong with just using <code>undefined</code> itself?</p> <p>In a perfect world we would be able to safely just use <code>undefined</code>: it's much simpler and easier to understand than <code>void 0</code>. But in case you've never noticed before, <em>this isn't a perfect world</em>, especially when it comes to Javascript. </p> <p>The problem with using <code>undefined</code> was that <code>undefined</code> is not a reserved word (<a href="https://web.archive.org/web/20160311231335/http://wtfjs.com/2010/02/15/undefined-is-mutable" rel="noreferrer">it is actually a property of the global object <sup>[wtfjs]</sup></a>). That is, <code>undefined</code> is a permissible variable name, so you could assign a new value to it at your own caprice.</p> <pre><code>alert(undefined); //alerts "undefined" var undefined = "new value"; alert(undefined) // alerts "new value" </code></pre> <p>Note: This is no longer a problem in any environment that supports ECMAScript 5 or newer (i.e. in practice everywhere but IE 8), which defines the <code>undefined</code> property of the global object as read-only (so it is only possible to shadow the variable in your own local scope). However, this information is still useful for backwards-compatibility purposes.</p> <pre><code>alert(window.hasOwnProperty('undefined')); // alerts "true" alert(window.undefined); // alerts "undefined" alert(undefined === window.undefined); // alerts "true" var undefined = "new value"; alert(undefined); // alerts "new value" alert(undefined === window.undefined); // alerts "false" </code></pre> <p><code>void</code>, on the other hand, cannot be overidden. <code>void 0</code> will <em>always</em> return <code>undefined</code>. <code>undefined</code>, on the other hand, can be whatever Mr. Javascript decides he wants it to be.</p> <h2>Why <code>void 0</code>, specifically?</h2> <p>Why should we use <code>void 0</code>? What's so special about <code>0</code>? Couldn't we just as easily use <code>1</code>, or <code>42</code>, or <code>1000000</code> or <code>"Hello, world!"</code>?</p> <p>And the answer is, yes, we could, and it would work just as well. The only benefit of passing in <code>0</code> instead of some other argument is that <code>0</code> is short and idiomatic.</p> <h2>Why is this still relevant?</h2> <p>Although <code>undefined</code> can generally be trusted in modern JavaScript environments, there is one trivial advantage of <code>void 0</code>: it's shorter. The difference is not enough to worry about when writing code but it can add up enough over large code bases that most code minifiers replace <code>undefined</code> with <code>void 0</code> to reduce the number of bytes sent to the browser.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload