18

This is an oddity I've seen occasionally in JS - maybe someone can shed light on it.

I do a test for undefined on a variable:

if (x !== 'undefined'){}

or even

if (typeof x !== 'undefined'){}

And the browser still throws an error:

ReferenceError: x is not defined

Even

if (x) {} 

throws the error.

This is a framework-level global variable I am checking for, so possibly something to do with different scopes. (No critiques of global variables - again, its the existence of a framework I'm testing for).

6
  • 7
    second one - typeof x !== 'undefined' should not throw a ReferenceError.
    – Anurag
    Commented Feb 20, 2011 at 19:51
  • 2
    Which browser/version are you working on ?
    – Premraj
    Commented Feb 20, 2011 at 19:55
  • 1
    Anurag: exactly - shouldn't but does
    – mtyson
    Commented Feb 20, 2011 at 20:45
  • Not possible: all major browsers support this correctly and never throw an error for typeof x for any x.
    – Tim Down
    Commented Feb 20, 2011 at 22:09
  • can you post a reproducible example on jsfiddle? also what is the configuration (browser/version/os) that it's occurring on if it makes a difference.
    – Anurag
    Commented Feb 21, 2011 at 7:36

3 Answers 3

12

That's pretty weird. What about:

if (window['x']) {
   // It's defined
}

Does the above work? Also, what browser or JavaScript interpreter is this?

3
  • The problem appears to be crossbrowser (IE/FF/Safari). Using the window['x'] actually works, I can test for !window['x'] without error. Somehow, the browser is testing x in one scope and erroring out in another.
    – mtyson
    Commented Feb 20, 2011 at 20:11
  • 1
    Want to add, even this method not worked, but window.x worked -_- , really weird. Commented Feb 18, 2014 at 21:12
  • See also the accepted answer here: "You'll have to define it, to be able to check it for a value." Maybe that should read: You'll have to declare it, to be able to check it for a value. if(variable) actually checks for a value and also works to check for possibly non-existing properties: if(obj.variable). If a 'naked' variable might not be declared, its existance can safely be checked with if (typeof(variable) == "undefined").
    – Don P
    Commented Nov 19, 2017 at 11:25
6

@Anurag is right the second condition should not through error.

if (typeof x !== 'undefined'){// won't through error even if x is not declared
}

I guess the problem is that x is not declared as a variable. If you declare it but leave it unassigned it is treated as undefined and won't through error, in your case it is not declared.

var x;
if (x !== undefined){// won't through error
}

Objects fields are treated differently

if(window.x !== undefined){// won't through error

}

it seems like x in this case is declared in runtime if not found, so returns undefined

Hope this helps!

2
  • I guess the important part here is the window.x !==undefined which i always use. It is okay to check for undefined in the window object, since they always are "declared" when someone checks for them... But that is not applicable to a normal scope though.
    – Miguel Q
    Commented Feb 21, 2017 at 18:31
  • @Miguel Overuse of that approach(3rd) will spoil the app with global variables which is a bad practice. Commented Feb 24, 2017 at 1:43
1

Your problem is that undefined !== 'undefined'

2
  • 3
    But typeof x === 'undefined' ;)
    – mtyson
    Commented Apr 30, 2013 at 22:39
  • Fast typing response error xD. I see your point now. As @anurag and @timDown said in FF 20.0.1 you don't get errors with typeof x !== 'undefined'. Good luck testing other browsers Commented May 1, 2013 at 16:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.