Filling an array with values

by Khương Lâm Ngọc

JavaScript

let values = Array.from({length: 3}); // Creating an Array with undefined
console.log(values); // [undefined, undefined, undefined]

values = Array.from({length: 3}, () => 0); // Creating an Array with small integers
console.log(values); // [ 0, 0, 0 ]

values = Array.from({length: 3}, () => ({})); // Creating an Array with unique (unshared) objects
console.log(values); // [ {}, {}, {} ]

values = Array.from({length: 3}, (x, i) => i); // Creating an Array with ascending integers
console.log(values); // [ 0, 1, 2 ]