3

In JavaScript this is true:

undefined == undefined

But this is false:

undefined <= undefined

At first I thought the <= operator included the first one, but I'm guessing it tries to cast it to a number and it fails, but I didn't find any documentation to back this up.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
pablopunk
  • 371
  • 1
  • 11
  • The first uses the [*abstract equality comparison algorithm*](http://ecma-international.org/ecma-262/8.0/#sec-abstract-equality-comparison), the second uses the [*abstract relational comparison algorithm*](http://ecma-international.org/ecma-262/8.0/#sec-abstract-relational-comparison). – RobG Jan 07 '18 at 20:36
  • @RobG sounds interesting, thanks! – pablopunk Jan 07 '18 at 20:40

2 Answers2

6

The <= operator coerces both operands into actual Numbers before performing the comparison if both operands are primitives -- while == does not.1 Abstract relational comparison is performed which is where this conversion actually happens. The operation ToNumber is performed on undefined which yields NaN (see the linked table). If you then look at steps 4c and 4d from Abstract relational comparison, if either operand of <= is coerced into NaN, then undefined is returned from Abstract relational comparison. Going back to the first link, you'll see in step 7:

If r is true or undefined, return false. Otherwise, return true.

Since Abstract relation comparison returned undefined, <= evaluates to false.

Less formally, you can see your comparison like this:

const first = Number(undefined); //or +undefined
const two = Number(undefined); //this is NaN
NaN <= NaN

Since NaN == NaN is never true, nor is NaN < NaN, NaN <= NaN is false.


1 undefined == undefined returns true based on the abstract SameValueNonNumber operation which is used with equality operators if both operads are the same value, but not numbers.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • Sounds right! I should have read ECMA standard first . I'm gonna mark this answer as correct, although I would add why the first comparison (undefined == undefined) is true, which is found in the third step here http://ecma-international.org/ecma-262/8.0/#sec-samevaluenonnumber – pablopunk Jan 07 '18 at 20:51
-1

Read about coercion in JavaScript. About your question, when you apply the equality operator your undefined is coerced to a Boolean. So you got a true result. When you apply the "<=" operator your undefined is coerced to a Number and you get a NaN value and this is the cause of your false result.

  • The statement *when you apply the equality operator your undefined is coerced to a Boolean* is incorrect. In the abstract equality comparison algorithm when values are of the same type, the strict equality comparison algorithm is used. And when the values are not numbers, the [*same value non number algorithm*](http://ecma-international.org/ecma-262/8.0/#sec-samevaluenonnumber) is used. The final result is boolean, but no there is coercion. ;-) – RobG Jan 07 '18 at 20:41