Can I use chained comparison operator syntax?

后端 未结 6 2116
醉话见心
醉话见心 2020-12-19 17:30

In one JS library I saw such syntax:

if (val > 5 == t) { ... }

I tested this in console:

1 == 1 == 2 // false
2 > 1 =         


        
6条回答
  •  伪装坚强ぢ
    2020-12-19 18:03

    It's a correct syntax but not one I would recommend. What's happening, is probably:

    if ((val > 5) == t) { ... }
    
    I tested this in console:
    
    (1 == 1) == 2 // false
    (2 > 1) == 1  // true
    (1 == 2) == 1 // false
    (1 == 1) == 1 // true
    (1 < 2) < 3   // true
    (1 > 2) > 3   // false
    

    With the boolean on the left implicitly converted to an int.

提交回复
热议问题