0

like normal math, I tried to log this code in the console and I expected to get the value true

 console.log(12>11>=10)

but what I got was false however when I tried to log console.log(12>11&&11>=10) I got true so for the last time, I tried console.log( (12>11&&11>=10) == (12>11>=10) )and i got flase

so my question is :

why in javascript (12>11&&11>=10) does not equal to (12>11>=10) ?!

and I hope anyone can help

Migo
  • 31
  • 4
  • 3
    Because you're asking if `true >= 10`. – jonrsharpe Aug 06 '20 at 13:07
  • Does this answer your question? [How to check if a number is between two values?](https://stackoverflow.com/questions/14718561/how-to-check-if-a-number-is-between-two-values) – Lain Aug 06 '20 at 13:09
  • Duplicate of [Javascript chained inequality gives bizarre results](https://stackoverflow.com/questions/15466099/javascript-chained-inequality-gives-bizarre-results). Alternatively, [Why does (0 < 5 < 3) return true?](https://stackoverflow.com/q/4089284/4642212), or pick any of its [linked questions](https://stackoverflow.com/questions/linked/4089284). – Sebastian Simon Aug 15 '20 at 03:37

2 Answers2

0

Because programming language syntax and human intuition are two very different things. At no point will the language "know what you mean", you have to be completely explicit and unambigous.

What you have here are two operations. One using the > operator and one using the >= operator. Operations are atomic things. One will happen, then the other.

So this:

12 > 11

Results in this:

true

And then this:

true >= 10

Results in this:

false

Use less intuition and more logic. Separate your two operations:

12 > 11
11 >= 10

And combine them logically:

(12 > 11) && (11 >= 10)

Which will evaluate to:

true && true

Which will evaluate to:

true
David
  • 208,112
  • 36
  • 198
  • 279
  • thank you I'm used to working with python so I didn't know the way that javascript will interpret this code (the successive 2 operators )by – Migo Aug 06 '20 at 13:22
0

Because of the bindings of the operators > and >= are the same this is handled as:

(12>10)>=10

Which means:

true>=10

And this is false.

Sascha
  • 4,576
  • 3
  • 13
  • 34