🟑 MediumType CoercionπŸ’» Output Question

Addition vs concatenation

πŸ’‘

Hint

+ is overloaded (concat/add). If either side is a string, it concatenates. - always forces numbers.

What does this output?

console.log(1 + '2');
console.log('3' - 1);
console.log(true + true);
console.log([] + []);
console.log([] + {});
console.log({} + []);

Correct Output

"12"
2
2
""
"[object Object]"
0

Why this output?

Explanation: 1+'2': number+string = string concat "12".
'3'-1: - forces numeric, 3-1=2.
true+true: true coerces to 1, 1+1=2.
[]+[]: both to string "", ""+"" = "".
[]+{}: ""+"[object Object]".
{}+[]: {} parsed as empty block, +[] = +0 = 0.

Key Insight: + is overloaded (concat/add). If either side is a string, it concatenates. - always forces numbers.

More Type Coercion Output Questions

🟒 EasyLoose equality coercionβ†’πŸŸ‘ MediumFalsy values comparisonβ†’πŸŸ‘ MediumNaN comparisonβ†’πŸ”΄ HardObject to primitive coercionβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz