-1

Possible Duplicate:
How to check for undefined or null variable in javascript

I want to check for an defined variable in javascript. Please see the following few examples and help me which is the best method to check 'a' for undefined (and check for nothing else) in Javascript?

one

if(a === undefined) { ... }

second

if(a === "undefined") { ... }

third

if(typeof a == "undefined") { ... }

last

if(a) { ... }

1
  • are you checking if variable has been defined or if it is undefined? i.e. variable is defined: var a; variable is undefined: var a; Commented Jul 23, 2012 at 3:19

3 Answers 3

2

if(typeof a == "undefined") { ... } is the best way to check if a variable is undefined.
if(a === undefined) { ... } is often the same thing, however, contrary to common belief "undefined" is NOT a keyword in javascript, and can in fact have a value assigned to it. Also, if a hasn't been declared or initialized an error will be thrown.

if(a === "undefined") { ... } will check if a is a string with the value of "undefined", and
if(a) { ... } will return true for all falsey values, such as null and 0.

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

4 Comments

you said that if(a === undefined) is normally the same thing. With which method are you comparing with? Secondly is it correct to say if(typeof a == "undefined")or this is correct if(typeof(a) == "undefined")?
if(a === undefined) is normally the same thing as if(typeof a == "undefined"). if(typeof a == "undefined") and if(typeof(a) == "undefined") are exactly the same. You can use whichever version you want.
@jeff—the first is not a good way to check since if a hasn't been declared or initialised an error will be thrown. The best way to check is if (typeof a == 'undefined'). Of course that doesn't distinguish between whether a has been created and assigned a value of undefined or hasn't been created at all. But then what is the point of that distinction anyway?
@RobG - Thanks for informing me about the error being thrown with the first method. I updated my answer.
2

undefined in javascript is an actual value, so if you do a === "undefined" you're just checking if a variable that is already defined has a value of undefined assigned to it. If the variable isn't hoisted yet then you'll get a nice error.

if(a) is checking if the value of a is true or false. undefined in JS as many other values is falsey so it acts as false. Still, if the variable hasn't been declared before then this will throw an error too.

typeof a === "undefined" is the correct way of checking for a variable that might not exist yet.

4 Comments

why do you think typeof a === "undefined" is the cirrect way of checking?
because it'll actually test if the variable exists or not, not if it's value is undefined or false like the other options.
Because it will not throw an error if a hasn't been initialised or declared, where as if (a) will.
@eclans—no, it doesn't. It will return true if either a hasn't been or if it has been declared or initialised and assigned the undefined value.
2
if (typeof a == "undefined") {
    ....
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.