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. Commented Feb 20, 2011 at 19:51
  • 2
    Which browser/version are you working on ? Commented Feb 20, 2011 at 19:55
  • 1
    Anurag: exactly - shouldn't but does 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. 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. 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?

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Want to add, even this method not worked, but window.x worked -_- , really weird.
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").
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 Comments

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 Overuse of that approach(3rd) will spoil the app with global variables which is a bad practice.
1

Your problem is that undefined !== 'undefined'

2 Comments

But typeof x === 'undefined' ;)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.