whatstheoutput_

#13: +'42px'

Topic: coercion · Difficulty: ★

console.log(+'42px');

Output

NaN

Why

Unary + tries to convert its operand to a number using the same algorithm as Number(). For strings, that algorithm requires the ENTIRE trimmed string to look like a valid numeric literal — leading whitespace and a plain digit run are fine, but any trailing non-numeric characters make the whole conversion fail. '42px' has a leading number but trailing letters, so the conversion doesn't just ignore the 'px' — it gives up entirely and returns NaN, unlike parseInt, which would happily stop at the first non-digit and return 42.

More coercion puzzles

Play today's puzzle →