Possible Duplicate:
What does “javascript:void(0)” mean?
What doesvoid 0
mean?
In the file http://underscorejs.org/underscore.js, you can see such a code block:
// Is a given variable undefined?
_.isUndefined = function(obj) {
return obj === void 0;
};
I don't understand the void 0
part, it's very strange. How to understand it?
This is the console output
>typeof void 0
"undefined"
>void 0 === undefined
true
>"undefined".length
9
>"void 0".length
6
I think they are trying to save 3 bytes of file size ;)
Edit:
This SO answer makes more sense of using void 0
, as undefined
is just a property of window
object and is mutable. Hence void 0
is a trusted way to generate undefined across browsers
In javascript (since 1.1), the void
operator is used to evaluate an expression and return undefined
.
See ECMAScript Language Specification of the void operator
So void 0
is a correct and standard way to produce undefined
.
As it is an operator, no parenthesis are needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With