snippet Saturday 20 April 2024

Create an array with values 0 to 100 using JavaScript

Author

This JavaScript code will create an array that is 101 in length with values 0-100

var myArray = [...Array(101).keys()]

There are many ways to do this but the shortest most concise way I have found is with the code below. It cleverly uses the Array constructors keys() method inside a spread operator with an array [] wrapping everything. The result of that gets stored into the myArray variable.