2
let x = 9;

if (5 < x < 9) {
    console.log('less than 9');
}

That code returns 'less than 9', but it is actually equal to 9. What is going on ?

DevMoutarde
  • 599
  • 7
  • 21
  • 2
    the above shows you how you should actually check this - your code is interpreted as `if ((5 < x) < 9)`, that is `if (true < 9)`, which doesn't really make sense (and happens to be `true` under JS's type conversion rules, I think the `true` is treated as the number 1. – Robin Zigmond Dec 01 '22 at 18:03
  • 1
    @RobinZigmond oh wow, never noticed that behaviour before, thats insane. So I should have used (x > 5 && (x < 9) instead right ? – DevMoutarde Dec 01 '22 at 18:05
  • you should write something like that: if (x > 5 && x < 9) {...your code}. The explenation is that JS has a "questionable" table truth. – Pikappa Dec 01 '22 at 18:06
  • Ok thank you all, we learn from JS everyday ahah – DevMoutarde Dec 01 '22 at 18:06
  • while there's plenty of strange behaviour in JS, I don't count this as an example of it. `<` like most operators is only designed to work with 2 values, and most languages won't accept `5 < x < 9` and have it work the way you expect. (Python iirc does have this work but I assume that's due to some special handling for this case in its syntax.) – Robin Zigmond Dec 01 '22 at 18:13

0 Answers0