Search⌘ K

Discussion: The Arrayist

Explore a concise JavaScript technique to generate arrays with numbers from 1 to n by combining arrow functions, the spread operator, and the map method. Understand how each part works together to transform empty arrays into useful numeric sequences and enhance your functional programming skills.

Verifying the output

Now, it’s time to execute the code and observe the output.

Javascript (babel-node)
const f = (n) => [...Array(n)].map((_, i) => i + 1);
console.log(f(20));

Understanding the output

This puzzle reveals a super concise method to generate an array of numbers from 1 to n using the powerful combination of arrow functions, the spread operator, and the map() method. ...