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.
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.
Share Improve this question edited Oct 14, 2013 at 20:04 Ultimate Gobblement asked Oct 14, 2013 at 19:17 Ultimate GobblementUltimate Gobblement 1,87916 silver badges26 bronze badges 3- Good question, and there is a good answer on SO : stackoverflow./questions/1291942/… – Sébastien Commented Oct 14, 2013 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 Commented Oct 14, 2013 at 19:43
- You're quite right, Sir. Try this one, 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 Commented Oct 14, 2013 at 19:50
2 Answers
Reset to default 11As per this page https://developer.mozilla/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 "Wele");
console.log(void(true));
console.log(void(0));
console.log(void("Wele"));
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./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.
void
also evaluates the expression you pass to it. It doesn't just return undefined
.