null and undefined inconsistent comparison
Asked Answered
T

2

8

I'm curious to know why

null == undefined

returns true but

null >= undefined

returns false

Is the inclusion of the greater than operator coercing the values differently?

Tymothy answered 3/12, 2015 at 0:38 Comment(8)
undefined is a variable, that happens to be undefined as long you don't define itAsymptomatic
I think coercion only works with "==". Edit: hmm interesting, false >= null actually returns true. So now I am curious as well ;)Vaniavanilla
Is the inclusion of the greater than operator coercing the values differently? The inclusion of > means the values DON'T get coerced.Jumbala
Sorry, what does 'coerced' mean in this context?Asymptomatic
Why would false >= null return true if >= does not coerce?Tymothy
@Jumbala but why does false >= null return "true" then?Vaniavanilla
Because something is greater than nothing.Jumbala
its not about greater, "greater" will not work. It only works bc its ">="Vaniavanilla
T
7

tl;dr The >= ends up coercing both arguments to numbers in this case: undefined gets coerced to NaN while null gets coerced to 0, which aren't equal. For ==, the spec explicitly defines that null == undefined is true.


The values do, in fact, get coerced in both cases (in a sense, at least - the case with == is special). Let's consider them one at a time, with the help of the spec.

The algorithm for the >= operator uses the "Abstract Relational Comparison Algorithm", which is shared by other relational operators. From the description in the spec, we see that the algorithm does the following:

  1. Converts the arguments to primitives (which null and undefined already are).
  2. Checks if the arguments are Strings (which they are not).
  3. If they are not Strings, the algorithm converts the arguments to numbers (see steps 3.a. and 3.b.) and performs the comparison with the results.

The last point is the key. From the ToNumber table, we see that undefined gets coerced to NaN, and the algorithm considers any comparison with NaN as being falsy (see steps 3.c. and 3.d.). Thus, null >= undefined is false.


For the other case, ==, the story is actually much simpler: the spec explicitly states that null == undefined is true as part of the "Abstract Equality Comparison Algorithm" (see steps 2. and 3.). Thus, null == undefined is true.

Thibaut answered 3/12, 2015 at 0:59 Comment(5)
Good explanation. So this also explains why false >= null results in "true", bc both values would be coerced to "0" then, correct?Vaniavanilla
@docta_faustus: Exactly.Thibaut
@frontend_dev: Yep, both false and null get coerced to +0.Thibaut
The Abstract Relational Comparison Algorithm Wow that's a gob full. Learnt something though, so nice one.Jumbala
alert(+null) // 0 alert(+undefined) // NaNAsymptomatic
C
0

In JS == operator coerces values to same type to compare, so 1=="1" is true. Use === operator for exact type matching

Castaneda answered 3/12, 2015 at 0:44 Comment(1)
Not really asking about the difference between == and == but rather the difference between == and >=Tymothy

© 2022 - 2024 — McMap. All rights reserved.