Search code examples
javascriptperformanceconsoleconsole.logcomparison-operators

JavaScript comparison operators issue : using 2 operators consecutively :why in JavaScript (12>11&&11>=10) does not equal to (12>11>=10)


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


Solution

  • 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