7
(0 <= 0 <= 0) === false
(-1 < 0 <= 0 <= 0) === true

What's going on here? Does Javascript actually have inequality chaining that's just wrong in some cases?

Bryan Head
  • 12,360
  • 5
  • 32
  • 50
  • Ah, you're right Felix. Sorry about that; I searched for a while before posting, but didn't see that one. Is there any way I, as the other, can close it as a duplicate directly? – Bryan Head Mar 18 '13 at 21:44

2 Answers2

10

Typed up the question and then was struck by the answer. Javascript does not have inequality chaining. Rather, 0 <= 0 <= 0 becomes true <= 0, which is evaluated as 1 <= 0. Indeed, 0 < 0 <= 0 evaluates to true.

Bryan Head
  • 12,360
  • 5
  • 32
  • 50
6

There is no chaining of operator but precedence. Here all the operators have the same priority so the operations are done from left to right.

When your comparison involves a boolean, the MDN explains how the comparison works :

If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.

This means the first operation is decomposed according to priorities as

((0 <= 0) <= 0)

which is

true <= false

which is

false

And the second one is

(true <= 0) <= 0

which is

false <= 0 

which is true.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758