5

I'm discovering odds of JavaScript comparisons. I wanted to find an example of how tricky comparisons may be, and how can cause bugs in some situations.

I thought about example where some input variable remains undefined, and is compared to zero. Because undefined is false when converted to Boolean, and zero is false when converted to Boolean I decided to test following code:

var x;
//Here x should be initialized but due to some circumstances is not
if(x == 0){
   //This should run
}

Surprisingly...

Boolean(undefined) //false
Boolean(0) //false
x //undefined
x == 0 //false

Why it's like that?

Landeeyo
  • 13,299
  • 14
  • 54
  • 92
  • 5
    Since `1` and `2` are both truthy, would you expect `1 == 2` to be `true`? – James Thorpe Nov 15 '16 at 17:18
  • 2
    What sort of answer are you expecting beyond "because the spec says so"? http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 – Quentin Nov 15 '16 at 17:18
  • Well `undefined != 0` and I guess that's just how it is. See there for the complete list of JS comparisons: https://dorey.github.io/JavaScript-Equality-Table/ – laurent Nov 15 '16 at 17:19
  • Basically the same reasoning as [Why does “undefined equals false” return false?](http://stackoverflow.com/q/19277458/215552) – Heretic Monkey Nov 15 '16 at 17:23

3 Answers3

9

This behaviour is in the specification for The Abstract Equality Comparison Algorithm

From the specification

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

  1. If Type(x) is the same as Type(y), then ... ...

  2. If x is null and y is undefined, return true.

  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  8. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
  10. Return false.

As undefined and a number (0) is not of the same type, it's only in the third point where it mentions what to do if the left hand side is undefined.
Then, if the right hand side is null, it returns true, any other value, and it goes straight to 10., which says "return false".

adeneo
  • 293,187
  • 26
  • 361
  • 361
  • Wow! That's amazing. If I'd compare undefined variable to any variable with already "known type" (so to speak) would it try to convert undefined to that type? – Landeeyo Nov 15 '16 at 17:21
  • 1
    @Landeeyo - actually no, it's a bit more complicated, the specification has the answer to this, and I posted a bit hasty, let me edit. – adeneo Nov 15 '16 at 17:27
  • This part of specification is stunning. Great answer. Thank you a lot! – Landeeyo Nov 15 '16 at 17:32
  • 1
    So in other words, comparing two "falsy" values of different type doesn't always return true, especially when the first value is `undefined`, then the only right hand value that is of an other type, and that would return true, is `null`. – adeneo Nov 15 '16 at 17:34
1
Boolean(undefined) //false
Boolean(0) //false

Actually, The Boolean function returns false for all non-real values such as 0, null, undefined, ""(empty string), etc.

It does not mean that the undefined == 0

Sankar
  • 6,182
  • 2
  • 23
  • 43
-6

This is not deep equal. If you want to compare the value and the type you can use triple '='

if(x === 0){
    //This should run
}

Otherwise undefined == null and so on.

user2693928
  • 3,158
  • 1
  • 11
  • 19