How to make sure undefined is not defined

Think about it, you write a plugin or a library (let’s name it jsKata) and your code contains checks to see if certain things are undefined.

Here’s an example :

  1. if (obj.name === undefined) {
  2.   obj.name = "This object has no name";
  3. }

This code works well until you include another script (found on an obscure website) and it breaks your previous code. By debugging the code, you see the value of undefined is no more undefined but false.

Yes, I know that you thought it was impossible to define undefined but it is, you just have to write undefined = false.

Redefine undefined

There’s two way of redefining undefined and they both use self-invoking functions.

Method 1 : Scope your code

  1. (function(undefined) {
  2.   /* your complete code here */
  3. })();

As you see, the self-invoking function has one parameter named undefined. At line 3, the self-invoking functions is called without any parameter. It results that the parameter has the value undefined (or it is defined to undefined if you prefer).

Take a look at this javascript :

It will display false and then display undefined even if undefined was globally defined at first.

This is the HTML that goes with it :

You can also execute it here.

Method 2 : Globally redefine undefined

There’s another method but I don’t like it that much. It redefines undefined but nothing protects it from being badly redefined in another chunk of code.

undefined is a global variable accessible using the global window object. Line 2 defines undefined and line 3 display the value of undefined as false. On line 4, I use a self-invoking function as method 1 does but now, I define the global undefined variable through the global window object.

The HTML of this example :

You can also execute it on jsFiddle.

Other methods

Some commentors pointed me other ways of doing it.

You can use typeof :

  1. typeof undefined == "undefined"

Or you could use void(0), it always returns undefined

  1. undefined === void(0) // this returns true
  • Guest

    Or you could just do `typeof myVar === “undefined”`.

  • http://www.javascriptkata.com dsimard

    yeah, that’s another option that I don’t like that much because I consider it “weak”… but I’ll update the post to mention it

  • Maynard

    you could always use variable == void 0

  • Anon

    Your two example code fragments are identical. You may need to update Method 2.

  • http://claimid.com/strager strager

    The `typeof` method also allows for testing of possibly-undefined identifiers, especially if you don’t know if `window` is the global object.  For example, `if (typeof exports !== ‘undefined’) { /* CommonJS */ }`.

  • Richard Ayotte

    Being able to define undefined must be bug. You can’t do undefined = false in Firefox 7 beta, it always returns undefined.

  • http://claimid.com/strager strager

    Note that `null == void 0` evaluates to `true`.  Use `variable === void 0`.

    Not to put down your method, but I haven’t seen `void 0` used at all in the wild outside of ‘academic’ examples, and I think it’d be harder to realize what `void 0` is *actually* doing (given its baggage in `javascript:` URI’s).  `variable === undefined` and `typeof variable === ‘undefined’` are the most common methods I see, and both work well.  (It’s a matter of style, in other words, except for one minor case.)

  • http://www.javascriptkata.com dsimard

    thanks for noticing it!

  • Anonymous

    Hey dsimard, why do you consider it weak?

  • http://www.javascriptkata.com dsimard

    I don’t know… maybe because it relies on a string. Maybe because of the typeof operator that I don’t like because I always write typeOf instead. I just prefer other solutions…

  • 李 鹏

    undefined = false;
    console.log(undefined.toString());//firebug console error “undefined has no properties”
    (function(undefined) { console.log(typeof undefined); })();

    why?