susanBuck / psy1903-forum

0 stars 0 forks source link

Confusion about operator practice problem #3 #7

Closed sylviatraw closed 1 month ago

sylviatraw commented 1 month ago

My code for this expression is console.log(20 > 15 < 15); and it outputs as true but I don't understand why it is true when 15 is not less than 15. This might just be a basic math confusion but I'm confused nonetheless lol

susanBuck commented 1 month ago

Great question! It has to do with the fact that when we treat boolean values as numbers in JavaScript, true translates to 1 (and false translates to 0).

So reading the statement left to right, 20 > 15 is true.

Then true < 15 is interpreted as 1 < 15 which is true, making the overall statement true.

Let us know if you have any follow-up questions on this!