When learning Underscore.js, I found that the following code often appears in the source code:
if (context === void 0) return func;
if (array == null) return void 0;
I haven’t seen this writing before. I went online to search for some information. I just found that some people asked similar questions on Stackoverflow. Summary here, take a note. Void is actually a function in JavaScript. Accepting a parameter, the return value is always UNDEFINED. It can be said that the purpose of using VOID is to get undefined in JavaScript. Sovoid 0
is a correct and standard way to produce undefined
.
void 0
void (0)
void "hello"
void (new Date())
//all will return undefined
Why don’t you use Undefined directly? There are two main reasons:
1. Use VOID 0 to reduce 3 bytes than using UNDEFINED. Although this is an advantage, personal meaning does not mean much, sacrificing readability and simplicity.
>"undefined".length
9
>"void 0".length
6
2、undefinedis not a reserved word in JavaScript, we can use itUNDEFINED as a variable name and assign it to it.
alert(undefined); //alerts "undefined"
var undefined = "new value";
alert(undefined) //alerts "new value"
Because of this, you cannot safely rely on undefined having the value that you expect。
void, on the other hand, cannot be overidden. void 0 will always return。
I tested under IE10, Firefox and Chrome. Unfortunately, there were no expected results. Although the above code has not been reported, it has not been printed “New Value” we expect.
So in general, the writing of VOID 0 is not significant.
Reference
http://stackoverflow.com/questions/7452341/what-does-void-0-mean