jskatas.org Continuously Learn JavaScript. Your Way.

Array API: array.fill()

Array.prototype.fill() can fill up an array with one value.

Array.prototype.fill can fill up an array with one value

fill(0) will populate 0 into each array element
const arr = new Array(3).fill(); assert.deepEqual(arr, [0, 0, 0]);
fill only changes content, adds no new elements
const arr = [undefined].fill(0); assert.deepEqual(arr, []);
second parameter to fill() is the position where to start filling
const fillPosition = 0; const arr = [1,2,3].fill(42, fillPosition); assert.deepEqual(arr, [1, 2, 42]);
third parameter is the position where filling stops
const fillStartAt = 1; const fillEndAt = 1; const arr = [1,2,3].fill(42, fillStartAt, fillEndAt); assert.deepEqual(arr, [1, 42, 3]);

Links

A discussion in a github issue, about how to use this kata.
API doc on MDN.

Required Knowledge

Related Katas

Array API

Difficulty Level

TBD

First Published

28 April 2015

Stats

4 tests to solve