bhylu.com

Advertisements

JS Array Constructor

Array literals are usually clearer than the Array constructor. Array.from can generate items from a length and a function.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

const seats = Array.from({length: 3}, (_, index) => index + 1);
console.log(seats);

What happens

The result is [1,2,3].