The difference between "void 0" and "undefined"

I use the "Closure Compiler" , while compiling my scripts I spend the following:

Before compiling:

// ==ClosureCompiler== // @compilation_level SIMPLE_OPTIMIZATIONS // @output_file_name default.js // @formatting pretty_print,print_input_delimiter // ==/ClosureCompiler== var myObj1 = (function() { var undefined; //<----- declare undefined this.test = function(value, arg1) { var exp = 0; arg1 = arg1 == undefined ? true : arg1; //<----- use declare undefined exp = (arg1) ? value * 5 : value * 10; return exp; }; return this; }).call({}); var myObj2 = (function() { this.test = function(value, arg1) { var exp = 0; arg1 = arg1 == undefined ? true : arg1; //<----- without declare undefined exp = (arg1) ? value * 5 : value * 10; return exp; }; return this; }).call({}); 

Compiled by:

 // Input 0 var myObj1 = function() { this.test = function(b, a) { a = a == void 0 ? true : a; //<----- var c = 0; return c = a ? b * 5 : b * 10 }; return this }.call({}), myObj2 = function() { this.test = function(b, a) { a = a == undefined ? true : a; //<----- var c = 0; return c = a ? b * 5 : b * 10 }; return this }.call({}); 

With this, I believe that the question of using "void 0" and "undefined" is there a difference in use or two cases is good ?.

Edit

if I define "var undefined" compiled with "void 0", if I do not define "undefined" compiled with "undedined." then there is no question of the number of characters between "undefined" and "void 0"

Test

Edit II: performance based on this link

Code and test

IE 8:
Type: 228ms
undefined: 62ms
void 0: 57ms

Firefox 3.6:
Type: 10 ms
undefined: 3ms
void 0: 3ms

Opera 11:
typeof: 67ms
undefined: 19ms
void 0: 20ms

Chrome 8:
Type: 3 ms
undefined: 5ms
void 0: 3ms

+55
javascript google-closure google-closure-compiler
Jan 26 2018-11-11T00:
source share
4 answers

from MDN :

The void operator evaluates this expression and then returns undefined .

This operator allows you to insert expressions that create side effects into those places where you need an expression that evaluates to undefined.

The void operator is often used simply to get a primitive value undefined , usually using " void (0) " (which is equivalent to " void 0 > " ). undefined ( , , ). void 0 > " ). undefined ( , , ).

The Closure compiler is void 0 because it contains fewer characters than undefined , therefore it creates equivalent, smaller code .




Re: Comment on OP

yes, I read the documentation, but in the example I gave, "google close" in the case of using "void 0" and another "undefined"

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

+62
Jan 26 '11 at 15:30
source share

The real only semantic difference between void expr and undefined is that in ECMAScript 3 the undefined property of the global object ( window.undefined in the browser environment) is writable, while the void operator will always return undefined .

A popular template that is often implemented, use undefined without worries - just declare an argument and pass nothing to it:

 (function (undefined) { //... if (foo !== undefined) { // ... } })(); 

This will allow minifiers to compress the argument, possibly by one letter (even shorter than void 0 :), for example:

 (function (a) { //... if (foo !== a) { // ... } })(); 
+51
Jan 26 2018-11-18T00:
source share

Just follow all the answers before that.

They look the same, but to the compiler they are completely different.

Two sections of code are compiled for different outputs, because one refers to a local variable (var undefined), and the compiler is simply in lines, because it is used exactly once and amounts to no more than one line. If it is used more than once, then this lining will not happen. Inside the lining provides a result of "undefined" that is shorter than "void 0".

Anyone who does not have a local variable refers to a variable named "undefined" under the global object, which is automatically "extern'ed" by the Closure compiler (in fact, all the global properties of the object). Therefore, the renaming does not occur, and no lining takes place. Voila! still "undefined".

+8
Mar 10 '11 at 12:33
source share

It makes no difference, try it yourself:

 void 0 === undefined 

will be evaluated to true .
undefined longer than 3 , which I think is why they use it that way.

+4
Jan 26 2018-11-11T00:
source share



All Articles