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?
Asked
Active
Viewed 63 times
-1

Rafid Muhymin Wafi
- 328
- 3
- 12
-
5Research: operator precedence. – jonrsharpe May 01 '22 at 17:05
-
Thank you @jonrsharpe. I will read more about that. – Rafid Muhymin Wafi May 01 '22 at 17:06
2 Answers
0
It is evaluating (1 + 1) ? 1 : (0 + 1).

Jordan Bonecutter
- 160
- 7
-
4Please refrain from answering questions which have obviously been asked before. – Heretic Monkey May 01 '22 at 17:08
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 ""

thisisnabi
- 1,045
- 9
- 21