Undefined = true; then go back to undefined?

Some javascript tomfoolery:

If it works

undefined = true;

then how can you go back to set undefinedback to the view undefined?


Of course, an easy way is to save undefinedin another variable before setting undefinedto true. What other way can you recover undefined?

At first I thought that it was delete undefined;not possible to return it .

+5
source share
2 answers

Alex's answer is a safe and practical way to guarantee undefinedtruly undefined.

JS , , undefined, window.undefined :

(function () {            // Create a new scope
   var a;                 // "a" is undefined in this scope
   window.undefined = a;  // Set the global "undefined" to "a"
})()                      // Execute immediately

, , :

undefined = (function () {
   var a;                 // "a" is undefined
   return a;              // return "a"
})();

, :

undefined = (function () {
    return;
})();

:

undefined = function(){}();

, void operator;-) doh!

undefined = void 0;
+8
(function(undefined) {

   // undefined is undefined again!

})();

jQuery .

delete . , , Array undefined (, in). splice() Array.

+7

All Articles