2

Several sources suggest, that there are two canonical ways to check whether variable is undefined:

foo === undefined
typeof foo === 'undefined'

But can anyone explain, why would one use === instead of == ?

EDIT: the question is not about === vs ==. It is about using correct operator with the 'undefined'. The difference between === and == is obvious. But the question is which operator would be more correct when checking if value is undefined or not.

0leg
  • 13,464
  • 16
  • 70
  • 94
  • 2
    `===` compares type *first* then value. `==` compares value and *may* convert the values so that the types match. – gen_Eric Nov 14 '14 at 21:43
  • 4
    check this: http://stackoverflow.com/q/359494/798682 – mattr Nov 14 '14 at 21:44
  • 1
    `==` performs type conversions to try and come up with a positive result. Especially for `typeof`, which only EVER returns a string, there's no need to convert types so why pay the overhead? – Chris Baker Nov 14 '14 at 21:45
  • `null == undefined`, but `null !== undefined`. – candu Nov 14 '14 at 23:07

4 Answers4

9

Sure simple. You base it off which behaviour you want (below)

null == undefined // true
undefined === null // false
typeof undefined // 'undefined'
typeof null // 'object'
megawac
  • 10,953
  • 5
  • 40
  • 61
4

=== is a strict comparison. === not only compares values but also datatype for example:

"2" == 2 is true
"2" === 2 is false
brso05
  • 13,142
  • 2
  • 21
  • 40
2

Looks like the question is not about the difference between == and === operators, but about in what situations one should use === undefined comparison and when typeof == 'unefined'. Well..

There are two ways to check for undefined value.

The first way is using strict comparison operator === to compare with undefined primitive:

var a;
a === undefined; // true

Above comparison will work as expected, only if the variable is declared but has undefined value. Note that if variable has never been declared you can't use a === undefined comparison because it will throw reference error:

a === undefined // ReferenceError: a is not defined 

That's why in this case typeof comparison is bullet-proof:

typeof a == 'undefined' // true

which will work properly in both cases: if variable has never been assigned a value, and if its value is actually undefined.

One more example. If we want to check for a prop property which is/can be missing:

someObj.prop === undefined // ReferenceError: a is not defined

but

typeof someObj.prop == 'undefined' // true
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • As long as `someObj` has been defined then attempting to check if `someObj.prop === undefined` will not throw a reference error, it'll just return `true` or `false` – Adam Jenkins Nov 14 '14 at 22:58
  • You are absolutely right. The question is NOT JUST about === vs ==. It is about using correct operator with the 'undefined'. It is a pity it got so many minuses since people rush to answer without thinking about the question. – 0leg Nov 15 '14 at 18:53
0

The === operator tests type as well as value. Using it consistently throughout your code helps to prevent a number of subtle and annoying errors, and is generally a Very Good Idea.

"5" == 5    // True, even though one is a string and the other a number
"5" === 5   // False because the two variables are of different type

While it's probably not strictly necessary when comparing to the special undefined property, it certainly doesn't hurt, and it's better to use === everywhere in your code than it would be to use === everywhere except for this corner case. Consistency is good.