Today I Learned

hashrocket A Hashrocket project

An ES6 javascript array of numbers

If you want an array of numbers in ruby, you might do something like (0..127).to_a. Concise! I don't really know how to do this in javascript but es6 has a couple of methods to help with a clever solution.

Array(100) gives you an array with 100 elements, all undefined.

array.keys() gives you an es6 iterator that will iterate through all the keys of the array, which happen to be 0 - 99.

Array.from() will turn an iterator into an Array!

So in just 3 weird steps you can get an array full of numbers.

Array.from(Array(127).keys())

Hopefully someone knows an easier way :).

See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.