Why Does 1 === 1 === 1 Evaluate to False, but 1 == 1 == 1 Evaluate to True?

Hey everyone,

I’ve been playing around with JavaScript and came across something weird. When I type 1 === 1 === 1 in the console, it evaluates to false, but 1 == 1 == 1 evaluates to true. Can anyone explain why this happens? Is the first comparison trying to evaluate the type of 1 === 1 as a whole? What is the reason for this behavior?

Thanks!
Emily

When you type 1 === 1 === 1, JavaScript evaluates it from left to right. First, it checks 1 === 1, which is true. Then it checks true === 1, which is false because true is a boolean and 1 is a number, and they are not strictly equal.

On the other hand, when you type 1 == 1 == 1, it also evaluates from left to right. First, 1 == 1 is true. Then it checks true == 1, which evaluates to true because the == operator performs type coercion, converting true to 1, and 1 == 1 is true.

So the key difference is that === checks for both value and type equality without type coercion, while == checks for value equality with type coercion

2 Likes

Welcome to the community tho :gift_heart::popcorn:

1 Like

Thank you very much dk