Why do chained comparison operators seem to work as "OR"?

Why do chained comparison operators seem to work as "OR"?
0

#1

This may be something without a short answer. But I just ran up against this fact:
2 < 6 < 4 -> true
I know I can work around it by saying 2 < 6 && 6 < 4, but now I’m curious about why it works that way.
This seems as if these two evaluations are “short-circuiting” as if with an OR between them, like 2 < 6 || 6 < 4. Is there an easy answer to why JS does this?


#2

I don’t think it’s short circuting as an OR

2 < 6 > 4    // returns false 

if it’s short-circuiting, it would have return true…

Don’t know why.


#3

I think what ends up happening is that the operation runs from left to right.

It evaluates the first part 2 < 6 as true

true is coerced into 1 in the second statement, so 1 < 4 evaluates as true

2 < 6 < 1 will return false


#4

But wait, how do you explain @owel’s example?

2 < 6 -> true
6 > 4 -> true
2 < 6 > 4 -> false

#5

Doesnt make sense. See my example above

2 < 6 > 4

1 < 6 is true
6 > 4 is true
AND YET…
2 < 6 > 4 is false!


#6

Because 1 > 4 is false


#7

Okay, here we go: http://stackoverflow.com/questions/4089284/why-does-0-5-3-return-true/


#8