Alex Rivera | Logout

difference between "void 0 " and "undefined"

Asked 2011-01-26T15:24:21.783
66

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

javascript google-closure google-closure-compiler

Edit
Report

1 Answer

4

There is no difference, Try it yourself:

void 0 === undefined

will evaluate to true.
undefined is 3 characters longer, I guess that is the reason why they use it that way.

answered 2011-01-26T15:28:00.413

Your Answer