What's the equivalent of a list comprehension like this one in ES2016 or greater?

Array comprehension in JS was proposed for ES2016, but never made it to the final release. Firefox supported comprehensions for a time, but the support was dropped in later versions.

You can use Array#from to get something close to comprehension.

const result = Array.from({ length: 5 }, (_, k) => `Cat #${k}`);

console.log(result);


There isn't anything so lovely in Javascript. To the best of my knowledge, you need to create a new Array and use .fill() to make each element something other than undefined. Then you can use .map and return/work with the array index rather than the value. Something like this:

console.log((new Array(5)).fill(0).map((x,i) => `Cat ${i}`))

You may find generators useful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Generator_comprehensions


console.log( [...Array(5)].map((v, i) => `Cat #${i}`) )

If it has to work in IE too :

console.log( Array.apply(0, Array(5)).map(function(v, i) { return 'Cat #' + i; }) )