Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript comparison operators

i came from a python background and came across an issue after doing a javascript project where

0 < 5 < 3

outputs to true which my guess is that it runs the first operator and completely disregards the second

and python it will render to false.

im not concerned about how to get this to work but its odd that its not in the documentation its only implied to do (0 < x && x < 3 ) and not keep it simple

guess my question is why is this like this if anyone can even answer

like image 285
cronicryo Avatar asked Dec 13 '25 12:12

cronicryo


1 Answers

In JavaScript, this:

0 < 5 < 3

is evaluated like this:

(0 < 5) < 3

which translates to this:

true < 3

which is further translated to this (because true is 1 in a numerical context):

1 < 3

which is true.


Python however was designed differently. As documented here, it interprets this:

0 < 5 < 3

as being equivalent to this:

0 < 5 and 5 < 3

The above expression can be further translated to this:

True and False

which is false.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!