Possible Duplicate:
Is there a standard function to check for null, undefined, or blank variables in JavaScript?
What is the best way to check undefined
type in javascript. I know 1 way to check for undefined type i.e. typeOf
. But i have to check if for lots of places, so if there is any short and better way to check then please let me know ?
I tried few ways but did`nt get success :
alert(undefined === "undefined");
alert(undefined || "defined");
Nothing new for you:
// either
(val === undefined)
// or
(typeof val == "undefined")
The problem of using val || "defined"
is that "defined" will be returned in case val
is null
, undefined
, 0
, false
or ""
.
That is the best way what you said using typeof.
Example:
alert(typeof variable === 'undefined')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With