9

Is there any differences between

var a;
(a == undefined)
(a === undefined)
((typeof a) == "undefined")
((typeof a) === "undefined")

Which one should we use?

JonnyReeves
  • 6,119
  • 2
  • 26
  • 28
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87

6 Answers6

11

Ironically, undefined can be redefined in JavaScript, not that anyone in their right mind would do that, for example:

undefined = "LOL!";

at which point all future equality checks against undefined will yeild unexpected results!

As for the difference between == and === (the equality operators), == will attempt to coerce values from one type to another, in English that means that 0 == "0" will evaluate to true even though the types differ (Number vs String) - developers tend to avoid this type of loose equality as it can lead to difficult to debug errors in your code.

As a result it's safest to use:

"undefined" === typeof a

When checking for undefinedness :)

JonnyReeves
  • 6,119
  • 2
  • 26
  • 28
  • 2
    ({tries undefined = true}) Who in their... why on... AAHG! WHY WOULD SOMEONE? That is a true failure of EcmaScript. I had never even thought to do that. – cwallenpoole Aug 18 '11 at 13:30
  • As per my example, 'for teh lolz!' The next iteration of EcmaScript (codenamed Harmony) will include support for constants, which can only be a good thing for our sanity ;) http://wiki.ecmascript.org/doku.php?id=harmony%3aconst – JonnyReeves Aug 18 '11 at 13:36
  • it is link to the first part of your answer: http://stackoverflow.com/questions/2703102/typeof-undefined-vs-null/2703131#2703131 – JohnJohnGa Aug 18 '11 at 14:43
1

I personally like to use

[undefined, null].indexOf(variable) > -1

to check also for null values.

Manuel Graf
  • 462
  • 5
  • 20
0

You should use mentioned above:

"undefined" === typeof a

But if you have lots of variables to check, your code can get ugly at some point, so you may consider to use Java's approach:

try { vvv=xxx; zzz=yyyy; mmm=nnn; ooo=ppp; } 
catch(err){ alert(err.message); }

Obviously alert() shouldn't be used in a production version, but in debugging version it's very useful. It should work even in old browsers like IE 6:

https://msdn.microsoft.com/library/4yahc5d8%28v=vs.94%29.aspx

DominikStyp
  • 360
  • 6
  • 10
0
var a;
(a == undefined) //true
(a === undefined) //true
((typeof a) == "undefined") //true
((typeof a) === "undefined") //true

BUT:

var a;
(a == "undefined") //false
(a === "undefined") //false
((typeof a) == undefined) //false
((typeof a) === undefined) //false
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • @JohnJohn well if they all yield the same thing there is no `difference`. But i believe the best bet is your 2nd option. – Naftali Aug 18 '11 at 13:29
-1

If a is undefined, then

a == undefined

will actually throw an error. Use

(typeof a) === "undefined"

Instead.

Chris Garaffa
  • 106
  • 1
  • 8
-2

If you declare var a, then it won't be undefined any more - it will be null instead. I usually use typeof a == undefined - and it works fine. This is especially useful in this situation:

function myfunc(myvar)
{
    if(typeof myvar == "undefined")
    {
        //the function was called without an argument, simply as myfunc()
    }
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265