Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for undefined type in javascript [duplicate]

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"); 
like image 546
Tom Rider Avatar asked Oct 19 '12 11:10

Tom Rider


2 Answers

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 "".

like image 154
VisioN Avatar answered Sep 21 '22 16:09

VisioN


That is the best way what you said using typeof.

Example:

alert(typeof variable === 'undefined')
like image 26
Snake Eyes Avatar answered Sep 19 '22 16:09

Snake Eyes