This page looks best with JavaScript enabled

Use Array Fill to Create Magic

 ·   ·  ☕ 1 min read

How to use Array.fill() to your fill.

We have seen examples of how to use Array.fill() to quickly fill data in an array. But, can we do more?

Yes..! You can generate array with sequential numbers. Use Array.fill() to line up your number array in sequence.

1
2
3
const nums = new Array(10).fill().map((val, index) => index + 1);
console.log(nums);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note technically an array.fill(), but you get it :)

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things