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;
void 0 is a correct and standard way to produce
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
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.