0

Aside from the obvious preventing of links being clicked (with javascript:void(0);) and for the definition of undefined in many javascript precompiled languages like coffeescript (where undefined becomes void 0) --> What else can the void function be used for?


Related: What does "javascript:void(0)" mean?

Useful Link: MDN page for void

Community
  • 1
  • 1
Naftali
  • 144,921
  • 39
  • 244
  • 303

1 Answers1

5

First, void is an operator, not a function. The answer to your question can't be given any more clearly than the explanation given in the link to MDN you provided in your question:

Summary

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

...

Uses

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.

Theoretical use:

Change the return value of an expression that produces side effects.

var i;
i = 0;       // returns 0
void i = 1;  // returns undefined

But this use is not very valuable in most situations.

Practical uses:

We already know about:

  • Cancel navigation in a bookmarklet
  • Compare item to undefined - Since the global variable undefined could be modified (it is not a reserved word), void 0 is a more reliable way to get an undefined value. Eg: obj.foo === void 0

Other uses:

Prevent verbose console output - I use it in a JavaScript console when I only want to execute some code and don't want to pollute the console with uninteresting verbose output.

Explicitly pass undefined to a function - It can be useful to know whether a function was called without passing arguments, or whether an argument was passed to a function with a value of undefined:

function countArguments(a) {
    console.log(a === undefined);
    console.log(arguments.length);
}
countArguments();
countArguments(void 0);

Your console output would be:

true
0
true
1

Does JavaScript need this operator? Probably not. You can get the same functionality (and more) from a self invoking anonymous function:

(function(){ /* expression */ })()

But that's not quite as concise.

Community
  • 1
  • 1
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • yea. so what does `void 1...n` do? or is it only `void 0` that does anything? – Naftali Jan 30 '13 at 19:21
  • No, because the expressions `0`, `1`, or `n` produce no side effects - those expressions don't do anything. `void 0` is essentially identical to `void 1` because there is no side effect and the return value is `undefined`. – gilly3 Jan 30 '13 at 19:42
  • So why does `void` even need an argument? – Naftali Jan 30 '13 at 19:42
  • Because it is an operator. An operator must *operate* on some expression. If it were a function, it would not necessarily need an expression. – gilly3 Jan 30 '13 at 19:46
  • 1
    Operators need no expressions! **Y U LIE** take `break;` or `continue;` for example – Naftali Jan 30 '13 at 19:47
  • 2
    @Neal `break` and `continue` are statements. Most operators are unary or binary, I never heard of nullary operators. – kapex Jan 30 '13 at 19:50
  • Still does not answer my question of why it **always** returns `undefined`.... where is the point in that? – Naftali Jan 30 '13 at 19:53
  • @Neal I think the reason is "the global variable undefined can be used instead (assuming it has not been assigned to a non-default value)." (see MDN). You cant be sure that `undefined` is really _undefined_. – kapex Jan 30 '13 at 19:56
  • 1
    The reason it always returns `undefined` is because [that's how the spec defines it](http://bclary.com/2004/11/07/#a-11.4.2). It sounds like you want to know why it exists. I don't know... convenience? Surely there are other ways to get the functionality it provides. Maybe one of the authors of ECMAScript-262 will come along and provide some insight. – gilly3 Jan 30 '13 at 20:09
  • "Since the global variable undefined could be modified (it is not a reserved word), void 0 is a more reliable way to get an undefined value" +1 – Christophe Marois May 11 '16 at 18:17