liveBook · Manning

Array.prototype.fill

As explained in Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A JavaScript array method that assigns a specified value into every element position of an array, including sparse slots, and returns the modified array.

A new Array(9) has nine holes, not nine values, so methods such as map skip those positions. fill assigns a specified value into every position, including holes. section 9.3

let arrayA = new Array(9).map(function() { return 1 });
let arrayB = new Array(9).fill(1);

Compare the mapped array, which remains empty, with fill, which creates an array containing nine 1s. section 9.3

How Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond covers it

How deep it goes
Explains why map skips holes in a new array and how fill populates them; does not detail other fill arguments.
Examples
Uses a nine-slot board and an array of nine 1s to demonstrate filling holes.
What you can do after
Apply Array.prototype.fill to populate a pre-sized array, including positions that are holes.
How it connects
Connects fill to sparse-array holes and contrasts it with map; groups it with Array.from and Array.of as array construction methods.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond. The section links point to the parts of the book it draws on.