Nothing can be simpler than comparing two numbers. However, there are some details that you need to know about comparing numbers in JS.

So, what happens under the hood when you write x === y and both x and y are numbers?

  • If x is NaN, return false
  • If y is NaN, return false
  • If x has the same value as y, return true
  • If x is 0 and y is βˆ’0, return true
  • If x is βˆ’0 and y is 0, return true
  • Else return false

2 interesting facts that you can take away from this if chain are:

  • If both numbers are NaN, the result is false. Should it be true instead?
  • If the numbers are 0 and -0, the result is true. Should it be false instead?

You can have your own opinion on how something should work, but that’s how things are in JavaScript world.

Sometimes you log two values to the console, they seem equal, yet when you compare them, the result is false. And sometimes it’s the other way around.