Should I use `void 0` or `undefined` in JavaScript [duplicate]

If you are using a modern browser, (which supports JavaScript 1.8.5) using undefined and void 0 would most likely yield the same result (since undefined is made not writable), except that the void can accept an expression as parameter and evaluate it.

In older browsers, (which do not support JavaScript 1.8.5) it is better to use void 0. Look at this example:

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

It will print

1

undefined is actually a global property - it's not a keyword. So, undefined can be changed, where as void is an operator, which cannot be overridden in JavaScript and always returns the value undefined. Just check this answer which I gave earlier today for a similar question, Why does void in Javascript require an argument?.

Conclusion:

So, if you are concerned about compatibility, it is better to go with void 0.


"void 0" is safer. "undefined" is a value, like any other. It's possible to overwrite that value with another:

undefined = 3;

That would change the meaning of your event.returnValue had you used undefined. "void" is a keyword, though, and its meaning can't be changed. "void 0" will always give the value undefined.