difference between “void 0 ” and “undefined”

From MDN:

The void operator evaluates the given expression and then returns undefined.

This operator allows inserting expressions that produce side effects into places where an expression that evaluates to undefined is desired.

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0“). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

Closure Compiler swaps in void 0 because it contains fewer characters than undefined, therefore producing equivalent, smaller code.


Re: OP comment

yes, I read the documentation, but in the example I gave, “google closure” in a case using “void 0” and another “undefined”

I believe this is actually a bug in Google Closure Compiler!

Leave a Comment