The fill(value, startIndex, endIndex) method

The fill() method of an array fills all the elements of the array from startIndex to endIndex (not including endIndex) with a given value. Remember that the startIndex and endIndex arguments are optional; therefore, if they are not provided then the whole array is filled with the given value.

If only startIndex is provided then endIndex defaults to the length of the array minus 1. If startIndex is negative then it's treated as the length of the array plus startIndex. If endIndex is negative, it is treated as the length of the array plus endIndex.

Here is an example to demonstrate this:

let arr1 = [1, 2, 3, 4];let arr2 = [1, 2, 3, 4];let arr3 = [1, 2, 3, 4];let arr4 = [1, 2, 3, 4];let arr5 = [1, ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.