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