5

As I understand the prefered way to check undefined variables is typeof a === 'undefined'.

But why it is better then typeof a == 'undefined'? In which places can it fail?

Paul Annekov
  • 3,193
  • 3
  • 19
  • 31

4 Answers4

10

In this instance, since typeof will always give you a string: It isn't better (nor is it worse). It makes no practical difference.

In general, using === is preferred because it forces you to be explicit about your types and saves you from getting results you don't expect when JavaScript's type resolution rules are unintuitive.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • +1. Do you think one will be slower than the other, or do JS engines know that it's a string comparison always and optimize? – Bergi Apr 22 '13 at 11:21
  • `===` will probably run faster and take longer to download, but the difference is so tiny that it isn't something you should worry about. (At least not until you are doing it thousands of times in a loop). Beware of premature optimization, it is the root of all evil. – Quentin Apr 22 '13 at 11:22
  • I don't think you can say strict equality is preferred "in general" since the vast majority of the code I've seen uses `==`. There are some influential persons in the javascript community who prefer one over the other, but it's up to programmers to learn the difference and use them appropriately. In javascript, the type of a variable is usually irrelevant, it's a loosely typed language where `===` can produce as many surprises as `==`, particularly when working with a DOM that sometimes returns values as strings and other times as numbers. – RobG Apr 22 '13 at 11:49
  • @Bergi—in the case where the operands have the same type, `==` and `===` perform exactly the same logic so any difference in speed (if there is one) is an artifact of the particular script engine and not a reliable (or useful) optimisation. – RobG Apr 23 '13 at 01:16
5

The difference between == and === is that == performs conversions. So for instance 1 will be == to '1' but not === to '1'. The reason why that approach is preferred when you check for undefined is because in JavaScript there are known comparison pitfalls.

The most common:

''        ==   '0'           //false
0         ==   ''            //true
0         ==   '0'           //true
false     ==   'false'       //false
false     ==   '0'           //true
false     ==   undefined     //false
false     ==   null          //false
null      ==   undefined     //true
" \t\r\n" ==   0             //true

So with === you are avoiding the null == undefined problem, which can lead to hard-to-find bugs. That's why you should use == instead of ===. Because === is not performing any conversions behind the scenes, it is also a faster operation.

In this specific case, it won't make a difference effect-wise. Whether you use typeof a == 'undefined' or typeof a === 'undefined' the output will be the same, with no bugs. That's because typeof returns a string. However, the operation will be faster, so you have a negligible performance increase.

flavian
  • 28,161
  • 11
  • 65
  • 105
3

Because typeof will only return string, so it is safe to compare two strings with ==.

xdazz
  • 158,678
  • 38
  • 247
  • 274
1

There is a big difference between == and === (Check out here)

But, since typeof will always return string, it's ok to use this.

Community
  • 1
  • 1
Stasel
  • 1,298
  • 1
  • 13
  • 26