Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.prototype.fill: filling a range with one value

fill mutates a selected receiver range, writes the same value at every index, and returns the receiver.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

array.fill(value[, start[, end]])

array.fill(value[, start[, end]]) writes value over the half-open range [start, end) and returns the same object. start defaults to 0, end to length; negative indexes count from the end, fractions are truncated, and bounds are clamped to length.

It fills sparse holes by creating properties. An object is not cloned: every index receives the same reference. The method is generic for writable array-like objects; a frozen array, read-only index, or proxy failure can throw TypeError after partial writes. An empty or reversed range changes nothing. The example exposes shared identity and a hole left outside the range.

Examples

const token = {kind: "shared"};
const values = new Array(3);
const returned = values.fill(token, 1);
console.log(0 in values);
console.log(values[1] === values[2]);
console.log(returned === values);

Result

false
true
true

Sources