3

EDIT: To clarify: My question isn't concerned with how void is used but whether void can be really useful. I'm asking this because the way people seem to use it is very offputting...

The MDN Reference for the void operator makes an example of the operator in use

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

Not exactly the sort of way we handle click events nowadays.

Is there a place for void in this world? Can someone show me a good example of using void?

Julian Krispel-Samsel
  • 6,792
  • 3
  • 31
  • 40
  • 2
    possible duplicate of [what is the point of void in javascript](http://stackoverflow.com/questions/666936/what-is-the-point-of-void-in-javascript) – Bergi Oct 21 '12 at 15:21

4 Answers4

5

Use case #1, void(0): If you want a reference to the real undefined (and not just the variable, because it can be overwritten) you don't need void. You can get it like this instead: (function(){}()).

Use case #2, void(exp): If you want to execute code and then return undefined, you can of course do it by wrapping your code in a function: (function(){ exp; return undefined; }()).

So, no, we don't need void. It does nothing unique. It is shorter than the above solutions though, so if you prefer short and obscure code you could use it (but please don't).

Jakob
  • 23,205
  • 7
  • 44
  • 55
2

Jakob shows some practical use cases of void. However as he mentions void is not really required, but I use it to test for undefined. I use it to create my own typeOf function:

function typeOf(value) {
    if (value === null) return "null";
    if (value === void(0)) return "undefined";
    return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}

You may read why I do so here.

Aadit M Shah
  • 67,342
  • 26
  • 146
  • 271
  • +1 for showing good use of void. I'm guessing your using void in case the context is an old browser where undefined is mutable? EDIT: Thanks for pointing me to that site, havdn't heard of it before... – Julian Krispel-Samsel Oct 21 '12 at 19:12
  • Guys, you missed the point of use case #1 in my answer. You dont need void for testing for `undefined`, because `(function(){}()) === void(0)`. So stating "void is not required, unless you wish to test for undefined" is plain wrong. – Jakob Oct 22 '12 at 12:18
  • Yes sir, you are correct. It is more succinct. Does that make the statement in this answer correct? A newbie reading only this answer will think that `void` is required to properly test for `undefined`. It is not. – Jakob Oct 22 '12 at 16:01
  • Btw, even if `void` was not built into the language it could be implemented in one line. `var void2 = function() {}`. It can be used like this `void2(0)` or like in the other example `void2(document.body.style.backgroundColor='green')`. It is a completely superfluous language construct. – Jakob Oct 22 '12 at 16:08
  • I couldn't agree more - `void` is not __required__ to test for `undefined`. I'll change that. Mind removing your down vote? – Aadit M Shah Oct 22 '12 at 17:03
1

void is an operator that is used to return a undefined value so the browser will not be able to load a new page. An important thing to note about the void operator is that it requires a value and cannot be used by itself.

Example:-

 <a href="javascript: void(0)">I am a link</a>

Ouput:-

 I am a link
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299
0

the void(0) is used to return "undefined", you can write there just "undefined", and also dont use that javascript: in href attribute - because its so old and it will not work for users with javascript disabled, also if someone open this in a new window they will see something like javascript:showPopup(27);