Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript expression bug (0 <= 14 < 10)?

Tags:

javascript

How can this be true?

0 <= 14 < 10

I need to evaluate that a number is in between 0 and 10.

But this breaks my logic.

Shouldn't that expression be false?

like image 520
ajsie Avatar asked Jan 27 '26 22:01

ajsie


1 Answers

This expression:

0 <= 14 < 10

is the same as

(0 <= 14) < 10

which is

1 < 10

true.

What you can do instead is:

if (0 <= x && x < 10) { ...

Python is the only programming language I can think of right now where the expression x < y < z does what you expect it to do.

like image 94
Greg Hewgill Avatar answered Jan 29 '26 12:01

Greg Hewgill



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!