#14: +[], +[5], +[5,6]
Topic: coercion · Difficulty: ★★
console.log(+[]);
console.log(+[5]);
console.log(+[5,6]);Output
0 5 NaN
Why
Unary + first converts the array to a primitive by joining its elements with commas, then converts that string to a number. An empty array joins to '', and Number('') is 0. A single-element array like [5] joins to '5', a clean numeric string, so it becomes 5. But [5,6] joins to '5,6', and a comma makes it an invalid numeric literal, so the whole thing collapses to NaN — proof that + on an array is really two coercions chained together: array-to-string, then string-to-number.
More coercion puzzles