Unary Plus On Arrays
luljs still worksA leading plus turns some arrays into numbers and others into NaN.
In 2026: Still works. Unary + coerces through ToNumber, which for an array means toString first. An empty array becomes "" then 0. A one-element array becomes that element's string then a number. A two-element array becomes "1,2", which is not a number, so NaN. A plain object becomes "[object Object]", also NaN.
Where it came from: Unary plus coerces through toString, so arrays flatten to a comma string before the number read. JavaScript Garden's types section walks through it. JavaScript Garden ↗
<script>
document.write('<p><code>+[]</code> is <b>' + (+[]) + '</b>, <code>+[5]</code> is <b>' + (+[5]) + '</b></p>');
document.write('<p><code>+[1, 2]</code> is <b>' + (+[1,2]) + '</b>, <code>+{}</code> is <b>' + (+{}) + '</b></p>');
document.write('<p>Unary + runs the value through toString, then tries to read a number.</p>');
</script>