Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

fill

Array.prototype.fill() replaces elements in a selected index range with one static value. It mutates the array and returns the modified array.

Since
ECMAScript 2015 (ES6)

How it works

Call fill() on an array with the value to write and, optionally, a start index and an exclusive end index. It writes the same value into each selected position, including holes in sparse arrays, mutates the original array, and returns that array.

Syntax

fill(value)
fill(value, start)
fill(value, start, end)

Parameters

NameTypeDescription
valueanyValue written into each selected array position.
startnumberOptional zero-based starting index, converted to an integer; defaults to 0 when omitted.
endnumberOptional zero-based exclusive ending index, converted to an integer; defaults to the array length when omitted or undefined.

Returns

The modified array, filled with value.

Key points

  • It replaces every selected element with the same supplied value.
  • The end boundary is exclusive, making fill(value, 1, 3) affect indices 1 and 2.
  • The optional start boundary is zero-based and converted to an integer before use.
  • The return value is the same array after its selected positions have been changed.
  • It can operate on array-like objects when the receiver supplies a length property.

Common mistakes

  • Filling with an object stores one shared reference in every selected slot; mutating the object through one slot affects the others.
  • The original array is changed, so use a copy first when preserving the input matters.

Example: Initialize an array

const slots = Array(4).fill(null);
slots[1] = "reserved";
console.log(slots); // [null, "reserved", null, null]

Creates four elements, then replaces one slot; fill() changes the original array and returns it.

When to use it

Initialize slots

Create a fixed-size array with an initial value, such as null placeholders.

Reset a range

Overwrite only a contiguous section of an existing array.

Fill array-like data

Populate an array-like object that exposes a length property.

Interview questions

QWhat does Array.prototype.fill() do?

fill() writes one value across an array index range and returns the modified array. The start index is inclusive, while the end index is exclusive.

Follow-up: What happens with negative bounds? They are interpreted relative to the array's end and then constrained to valid positions.

QDoes fill() mutate the array or alter its length?

The method mutates the receiver but does not change its length. If no bounds are supplied, it fills from index 0 through the final index.

Follow-up: How would you avoid mutation? Create a shallow copy first, such as [...array], and call fill() on that copy.

QWhat reference behavior occurs when filling with an object?

value can be any value, including an object. Every selected slot receives that exact value, so object values are shared references rather than separately cloned objects.

Follow-up: What is a safe way to create independent objects per slot? Use Array.from with a mapping function instead of filling with one object.

  • Array
  • Array.prototype
  • Array.from
  • TypedArray.prototype.fill

Source: Array.prototype.fill() - JavaScript | MDN Web Docs. Checked 2026-10-15.