Why does (10<3<10)
return true in Javascript? 3<10
but 10<3
is false. So it should return false
Asked
Active
Viewed 245 times
3

AlieN
- 555
- 2
- 16
-
2Actually, the reason it should return false is because 10 cannot possibly `<3` being less than 10 because 10 *is not less than* 10. – BoltClock Apr 11 '14 at 17:50
-
Most languages don't support the double comparison like that. – Sam G-H Apr 11 '14 at 17:50
-
You can't really use two operators, so operator presedence kicks in, and it's read `( (10<3) < 10 )` which equals `( false < 10 )` which is true – adeneo Apr 11 '14 at 17:50
-
@BoltClock, `4<3<10` also true) – vp_arth Apr 11 '14 at 17:51
-
This has been asked and answered here on SO at least once before, but I don't have the link handy. *Edit*: Oh, hey, it was right there in the "related" list. Which means it was on the "Your question may already have an answer" list shown to you before you posted the question. – T.J. Crowder Apr 11 '14 at 17:52
-
I am sorry. I am a newbie))) (registered yesterday)! – AlieN Apr 11 '14 at 17:57
-
because zero is less than 3... – dandavis Apr 11 '14 at 18:00
2 Answers
11
10<3<10
is equivalent to (10<3)<10
. 10<3
is false
, false<10
is true.

hobbs
- 223,387
- 19
- 210
- 288
-
-
-
-
@TimS.: Yes. The relational operators can act on strings or numbers, and prefer numbers. So if both sides support being converted to numbers (and bools do), it'll go with that. – T.J. Crowder Apr 11 '14 at 17:53
-
yup! Which is very strange for me, and PHP acts like that too. I like Ruby (which this kind of nonsense doesn't happen)! – Vitor Tyburski Apr 11 '14 at 17:53
-