create new array from 0 to 10 es6 in javascript

To create a new array from 0 to 10, you can use the spread operator (...) along with the Array.from() method in ES6 as follows:

index.tsx
const newArray = [...Array(11).keys()];

console.log(newArray);
// Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
110 chars
5 lines

This code creates an array of length 11 using the Array constructor and keys() method. The keys() method returns an iterator that generates the integer keys (indexes) in the range of 0 to 10.

The spread operator ... is then used to expand the contents of this iterator into a new array and assign it to the newArray variable. Finally, the contents of newArray are logged to the console.

Note that the keys() method doesn't work in all browsers, so you may need to use a polyfill or another approach if you need to support older browsers.

gistlibby LogSnag