strange results javascript comparison

前端 未结 1 1730
后悔当初
后悔当初 2021-01-17 08:19

I have an entry level question for javascript. When executing the following the result is
false
true
Some numbers are equal
number, number

numbernu

相关标签:
1条回答
  • 2021-01-17 08:48

    To do compound comparisons in JavaScript (or any other language syntactically derived from B), you don't do things like

    (l>k>m)        // <=== Wrong
    

    Instead, if you want to know if l is greater than k and k is greater than m, you use && (the logical "and" operator), like this:

    (l>k && k>m)
    

    Details:

    Your original expression (l>k>m) breaks down into this:

    ((l>k)>m)
    

    which means you'll get one of these:

    (true>m)
    // or
    (false>m)
    

    When comparing a boolean to a number, the boolean is coerced to a number, so that becomes in effect:

    (1>m)
    // or
    (0>m)
    
    0 讨论(0)
提交回复
热议问题