Skip to content

OutputNot attempted

Arithmetic with strings, null, undefined

console.log(1 + '2', '3' - 1, '3' * '4', true + 1, null + 1, undefined + 1, +'', +' 12 ', +'1a');

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

If either side of + is a string, it concatenates. Every other arithmetic operator converts to numbers. null → 0, undefined → NaN, "" → 0, unary + trims whitespace, but any junk ("1a") → NaN.