Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between "void 0 " and "undefined"

I'm using "Closure Compiler", when 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:

// 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 the question of the use of "void 0 " and "undefined", is there any difference in the use or the two cases are well?.

Edit

if I define "var undefined" compiled with "void 0 ", if I did not define "undefined" compiled with "undedined. " then not a matter of number of characters between "undefined" and "void 0"

Test

Edit II: performance, based on this link

Code and Test

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

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

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

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

like image 821
andres descalzo Avatar asked Jan 26 '11 15:01

andres descalzo


People also ask

Is void 0 same as undefined?

In JavaScript code, especially in older legacy code, you sometimes find the expression void 0 . The void operator evaluates an expression and returns the primitive value undefined. void 0 evaluates 0 , which does nothing, and then returns undefined . It is effectively an alias for undefined .

What is the meaning of void 0?

JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.

Is JavaScript void 0 Safe?

Using javascript: , you can run code that does not change the current page. This, used with void(0) means, do nothing - don't reload, don't navigate, do not run any code.

What is difference between null or void in JavaScript?

void <expression> It is an operator. Unlike both undefined and null , it does not represent a primitive value. The connection between void and the other two is that it always returns the value of undefined . Its purpose is to evaluate an expression (usually for its side effects) and then return undefined .


1 Answers

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!

like image 147
Matt Ball Avatar answered Sep 29 '22 21:09

Matt Ball