MediumModern JS💻 Output Question

Array.from with mapping function as range factory

💡

Hint

Array.from({length:n}, (_, i) => expr) is the idiomatic way to create filled arrays of specific length.

What does this output?

const zeros = Array.from({ length: 3 }, () => 0);
const range = Array.from({ length: 5 }, (_, i) => i + 1);
console.log(zeros.join(','));
console.log(range.join(','));

Correct Output

0,0,0
1,2,3,4,5

Why this output?

Explanation: Array.from with a length property creates an array. The second argument maps each element — _ is the value (undefined), i is the index.

Key Insight: Array.from({length:n}, (_, i) => expr) is the idiomatic way to create filled arrays of specific length.

More Modern JS Output Questions

EasyDestructuring defaults apply only for undefinedEasyObject spread — later properties winMediumOptional chaining prevents TypeError on missing propertiesMediumNullish coalescing ?? vs OR ||

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz