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) { … }
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, ifahasn’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”, andif(a) { ... }will return true for all falsey values, such asnulland 0.