-1

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?

2 Answers2

0

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

0

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

thisisnabi
  • 1,045
  • 9
  • 21