0

Why does this not work? if(10<=a<=15) in js

Code let a=20 if (10<=a<=15){ console.log("hello") } It still prints hello https://code.sololearn.com/Wx34itpKvPPi/?ref=app

22nd Sep 2022, 10:15 AM
StardustšŸ‹šŸ‡®šŸ‡³
StardustšŸ‹šŸ‡®šŸ‡³ - avatar
3 Answers
+ 8
The expression 10 <= a <= 15 is evaluated from left to right. Here I added parentheses to get you to understand, how the expression evaluation was performed. ( 10 <= a ) <= 15 ( true ) <= 15 ( 1 ) <= 15 This eventually, evaluates to true, hence the `if` block was entered. You probably were trying to achieve this if( 10 <= a && a <= 15 )
22nd Sep 2022, 10:36 AM
Ipang
+ 4
let a=20 //10<=a -> True //True<=15 -> True if (10<a<15==true==1) { console.log("HELLO!šŸ˜‰") }
22nd Sep 2022, 10:24 AM
Solo
Solo - avatar
+ 2
Ipang Solo Thank you so much! I understood :)
22nd Sep 2022, 2:47 PM
StardustšŸ‹šŸ‡®šŸ‡³
StardustšŸ‹šŸ‡®šŸ‡³ - avatar