Creating an Empty Array in Javascript

I needed it recently, for example I have a selectbox to list the days of the month from 1-31, but I needed to create an array with numbers from 1 to 31.

If I create it like this;

new Array(31)

but I have to fill it myself. Instead, a solution like this would make more sense;

const numbers = Array.from({ length: 31 }, (_, i) => i + 1)
console.log(numbers)

now you have an array with numbers from 1 to 31, I usually use it to fill in the select in react projects, it will definitely be useful for you one day :)

Comments

There are no comments, make the firs comment