10

From what I understand, the keyword void in Javascript is some kind of function that takes one argument and always returns the undefined value. For some reason you need to pass it an argument; it won't work without one.

Is there any reason why it requires this argument?

What is the point? Why won't it work without an argument. The only use I have seen for it is to produce an undefined result. Are there any other uses for it?

If not then it would seem that the requirement for an expression to be passed would be pointless.

Ultimate Gobblement
  • 1,851
  • 16
  • 23
  • Good question, and there is a good answer on SO : http://stackoverflow.com/questions/1291942/what-does-javascriptvoid0-mean – Sébastien Oct 14 '13 at 19:39
  • I only skim read it, but I can't see a post that answers my question in particular. Have I missed something? – Ultimate Gobblement Oct 14 '13 at 19:43
  • You're quite right, Sir. Try [this one](http://stackoverflow.com/a/7452352/1347953), or bet yet don't click: "Why void 0, specifically? Why should we use void 0? What's so special about 0? Couldn't we just as easily use 1, or 42, or 1000000 or "Hello, world!"? And the answer is, yes, we could, and it would work just as well. The only benefit of passing in 0 instead of some other argument is that 0 is short and idiomatic. – Sébastien Oct 14 '13 at 19:50

2 Answers2

10

As per this page https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void void is an operator, which simply returns undefined, after evaluating the expression you pass to it. An operator needs an operand to operate on. That is why pass a parameter.

console.log(void true);
console.log(void 0);
console.log(void "Welcome");
console.log(void(true));
console.log(void(0));
console.log(void("Welcome"));

All these statements would print undefined

var a = 1, b = 2;
void(a = a + b)
console.log(a);

And this would print 3. So, it is evident that, it evaluates the expressions we pass to it.

Edit: As I learn from this answer https://stackoverflow.com/a/7452352/1903116

undefined is just a global property which can be written to. For example,

console.log(undefined);
var undefined = 1;
console.log(undefined);

It prints

undefined
1

So, if you want to absolutely make sure that the undefined is used, you can use void operator. As it is an operator, it cannot be overridden in javascript.

Community
  • 1
  • 1
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 1
    Wouldn't the expression in parenthesis be evaluates before being passed to void. I could write a = 1, b = 2; Math.min(a = a+b, 2); and the expression would be evaluated before being passed. – Ultimate Gobblement Oct 14 '13 at 19:37
  • @UltimateGobblement Thats exactly it does. Unlike other operators, it doesnt do anything else. Simply evaluates the expression and returns `undefined`. – thefourtheye Oct 14 '13 at 19:45
  • @UltimateGobblement Please check my answer my now. I have added another usecase for `void`. – thefourtheye Oct 14 '13 at 19:52
  • You are explaining what void does and why to use it, but that was never my question. – Ultimate Gobblement Oct 14 '13 at 19:54
  • The original question is answered in the first paragraph of my answer I believe. `void` is an operator just like `+`, `-` etc. Except that it is an unary operator. Since, it is an operator, it needs an operator to operate on. Thats why we pass a parameter. It doesnt matter what we pass, it will always return `undefined`. – thefourtheye Oct 14 '13 at 19:56
  • OK I updated the question, hopefully it should help clarify what I'm asking here. – Ultimate Gobblement Oct 14 '13 at 20:05
  • Agree with @UltimateGobblement - void doesn't evaluate anything, that expression is evaluated before it is passed into void as a scalar argument. (this could also be the very definition of "Technicality", but still) – Tasos Bitsios Jul 16 '14 at 20:54
3

void also evaluates the expression you pass to it. It doesn't just return undefined.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • I still don't get what the point of evaluating the expression would be though? Can you provide an example where it may serve a purpose? – Ultimate Gobblement Oct 14 '13 at 19:38
  • @UltimateGobblement There's an example on MDN. See the section on JavaScript URIs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void – Bill the Lizard Oct 14 '13 at 21:23
  • 2
    Technically, this is incorrect. void evaluates nothing - the evaluation of the expression happens first, and its result is given to void, same as with all other unary operators (typeof, instanceof). The reason for this seems to be a technicality - JS has unary operators (one argument - typeof a, a++..) and binary operators (a + b) but no nullary operator (as void would be if it didn't accept an arg). More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators – Tasos Bitsios Jul 16 '14 at 20:49
  • @TasosBitsios No, technically it's right. http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.2 – Bill the Lizard Jul 16 '14 at 22:00
  • 1
    I think that ("1. Let ref be the result of evaluating MemberExpression. ") is just the phrasing. The same wording is on the function description ("http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.3"), but wouldn't you say that the argument expressions are evaluated before being passed into a function? – Tasos Bitsios Jul 16 '14 at 23:11
  • 1
    @UltimateGobblement If the expression has side effects, they will be executed. – Barmar Nov 01 '16 at 11:48