whatstheoutput_

#30: Array(3).length and Array(3) + ''

Topic: arrays · Difficulty: ★★

console.log(Array(3).length);
console.log(Array(3) + '');

Output

3
,,

Why

Array(3) with a single numeric argument doesn't create an array containing the number 3 — it creates a sparse array with length 3 and no actual elements ('empty slots'), which is why .length reports 3. When that array is coerced to a string (here via + ''), Array.prototype.toString joins its elements with commas, and each empty slot contributes nothing but its separator — three empty items joined by two commas produces the string ',,' (2 commas for 3 slots), which looks like garbage until you realize it's literally three empty strings glued together.

More arrays puzzles

Play today's puzzle →