'Why does 1 + 1 ? 1 : 0 + 1 return 1 instead of 3? [duplicate]

Why is 1 + 1 ? 1 : 0 + 1 equal to 1 and not 3? It's correct that it returns 3 if I wrap the expression 1 ? 1 : 0 with parentheses. But why is that needed? What is actually blocking the process to parse it correctly?



Solution 1:[1]

Because js first calculate 1 + 1 and after that check RESULT is true? if it is true, select 1.

1+1 is the truthy result

The falsy values in JavaScript are 0, 0n, null, undefined, false, NaN, and the empty string ""

enter image description here

Solution 2:[2]

It is evaluating (1 + 1) ? 1 : (0 + 1).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 thisisnabi
Solution 2 Jordan Bonecutter