11

It appears that window.undefined is writable, i.e. it can be set to something else than its default value (which is, unsurprisingly, undefined).

The point is however that whenever I refer to undefined, it refers to window.undefined (as window can be removed in cases like this).

So how do I actually get access to an undefined 'instance', so to say? How would I be able to set another variable to undefined, if window.undefined has been changed?

If I code:

window.undefined = 'foo'; // This code might have been executed by someone/something
var blah = undefined; // blah is not undefined, but equals to 'foo' instead...

How could I possibly solve this?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    Fix the code that's overwriting `undefined`? – John Kugelman Feb 16 '11 at 19:29
  • My guess would be store it before you change it. `var undef = window.undefined; window.undefined='foo'; car myvar = undef;` – Brad Christie Feb 16 '11 at 19:29
  • His point is `This code might have been executed by someone/something`. Where to take the value `undefined` from then? – Martin Hennings Feb 16 '11 at 19:31
  • @John: No I don't really mean that - I'd like to make my code safe and stable and not reliable on `window.undefined`. E.g., someone else could set `window.undefined` and then my code gets executed. Then is there any way to still get `undefined`? – pimvdb Feb 16 '11 at 19:31
  • Are you actually running into this problem in the field or just in theory/testing? While window.undefined is writable, obviously writing to it is a really bad idea. Are there libraries out there that do write to it? – Samuel Neff Feb 16 '11 at 19:41
  • Just theoretically - sorry if that confused anybody. – pimvdb Feb 16 '11 at 19:44
  • 1
    Maybe it's worth pointing out that modern browsers with full support of ECMA 5 do no longer allow you to override reserved keywords like e.g. `undefined` so this could not be a case then. – Pawel Sledzikowski May 14 '13 at 10:35

4 Answers4

19

The "standard" solution to this problem is to use the built in void operator. Its only purpose is to return undefined:

var my_undefined = void 0;

In addition to this, yhere are other ways to get undefined:

Functions return undefined if you don't return anything so you could do something like

this_is_undefined = (function(){}());

You also get undefined if you don't pass enough arguments to a function. So a common idiom is

function foo(arg1, arg2, undefined){ //undefined is the last argument
    //Use `undefined` here without worrying.
    //It is a local variable so no one else can overwrite it
}
foo(arg1, arg2);
//since you didn't pass the 3rd argument,
//the local variable `undefined` in foo is set to the real `undefined`.

This kind is particularly good for cases when you define and call the function at the same time so you don't have any risk of forgetting and passing the wrong number of arguments latter.

hugomg
  • 68,213
  • 24
  • 160
  • 246
17

In addition to the other solutions, you can do the void 0 trick, which always returns undefined irrespective of the window property.

window.undefined = 'foo';
var blah = void 0;

alert( blah );  // undefined
user113716
  • 318,772
  • 63
  • 451
  • 440
3

Actually, comparing anything with undefined is not good idea at all. Should use typeof operator instead:

function isUndefined ( variant ) { return typeof variant === 'undefined' }
Free Consulting
  • 4,300
  • 1
  • 29
  • 50
3

It's should be enough to just declare a variable without assigning it to anything:

var local_undefined;
alert(typeof local_undefined);

But why on Earth is it possible to change undefined value? Does anyone know history behind this?

Yury Semikhatsky
  • 2,144
  • 13
  • 12
  • It does work this way, but it is necessary to use `local_undefined` instead (http://jsfiddle.net/vMJYd/). And this issue seems to be fixed in JavaScript 1.8.5 (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined). – pimvdb Jul 08 '11 at 11:19
  • 1
    Correct link here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined – jontro Sep 26 '12 at 08:41