void 0 is effectively a compile time bulletproof constant for undefined with no look-up requirements. It is
like writing null or true, instead of looking up a variable value. It works out of the box without any safety arguments and is shorter to write. It is better in every way.
The difference is that some browsers allow you to overwrite the value of
undefined
. However,void(anything)
always returns realundefined
.JS is very loose at syntax, parentheses here are optional,
void 0
andvoid(0)
are equivalent.For second question, you need to use
undefined
directly while avoiding unneeded operand evaluation to retrieve the sameundefined
value.More info in the reference: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void
Use
undefined
. Its more commonly known thanvoid(0)
.undefined
has normal variable semantics that not even strict mode can fix and requires run-time look-up. It can be shadowed like any other variable, and the default global variableundefined
is not read-only in ES3.void 0
is effectively a compile time bulletproof constant forundefined
with no look-up requirements. It is like writingnull
ortrue
, instead of looking up a variable value. It works out of the box without any safety arguments and is shorter to write. It is better in every way.