Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Array.prototype.fill()

BaselineWidely available

Thefill() method ofArray instances changes all elements within a range of indices in an array to a static value. It returns the modified array.

Try it

const array1 = [1, 2, 3, 4];// Fill with 0 from position 2 until position 4console.log(array1.fill(0, 2, 4));// Expected output: Array [1, 2, 0, 0]// Fill with 5 from position 1console.log(array1.fill(5, 1));// Expected output: Array [1, 5, 5, 5]console.log(array1.fill(6));// Expected output: Array [6, 6, 6, 6]

Syntax

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

Parameters

value

Value to fill the array with. Note all elements in the array will be this exact value: ifvalue is an object, each slot in the array will reference that object.

startOptional

Zero-based index at which to start filling,converted to an integer.

  • Negative index counts back from the end of the array — if-array.length <= start < 0,start + array.length is used.
  • Ifstart < -array.length orstart is omitted,0 is used.
  • Ifstart >= array.length, no index is filled.
endOptional

Zero-based index at which to end filling,converted to an integer.fill() fills up to but not includingend.

  • Negative index counts back from the end of the array — if-array.length <= end < 0,end + array.length is used.
  • Ifend < -array.length,0 is used.
  • Ifend >= array.length orend is omitted orundefined,array.length is used, causing all indices until the end to be filled.
  • Ifend implies a position before or at the position thatstart implies, nothing is filled.

Return value

The modified array, filled withvalue.

Description

Thefill() method is amutating method. It does not alter the length ofthis, but it will change the content ofthis.

Thefill() method fills empty slots insparse arrays withvalue as well.

Thefill() method isgeneric. It only expects thethis value to have alength property. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

Note:UsingArray.prototype.fill() on an empty array (length = 0) would not modify it as the array has nothing to be modified.To useArray.prototype.fill() when declaring an array, make sure the array has non-zerolength.See example.

Examples

Using fill()

js
console.log([1, 2, 3].fill(4)); // [4, 4, 4]console.log([1, 2, 3].fill(4, 1)); // [1, 4, 4]console.log([1, 2, 3].fill(4, 1, 2)); // [1, 4, 3]console.log([1, 2, 3].fill(4, 1, 1)); // [1, 2, 3]console.log([1, 2, 3].fill(4, 3, 3)); // [1, 2, 3]console.log([1, 2, 3].fill(4, -3, -2)); // [4, 2, 3]console.log([1, 2, 3].fill(4, NaN, NaN)); // [1, 2, 3]console.log([1, 2, 3].fill(4, 3, 5)); // [1, 2, 3]console.log(Array(3).fill(4)); // [4, 4, 4]// A single object, referenced by each slot of the array:const arr = Array(3).fill({}); // [{}, {}, {}]arr[0].hi = "hi"; // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]

Using fill() to create a matrix of all 1

This example shows how to create a matrix of all 1, like theones() function of Octave or MATLAB.

js
const arr = new Array(3);for (let i = 0; i < arr.length; i++) {  arr[i] = new Array(4).fill(1); // Creating an array of size 4 and filled of 1}arr[0][0] = 10;console.log(arr[0][0]); // 10console.log(arr[1][0]); // 1console.log(arr[2][0]); // 1

Using fill() to populate an empty array

This example shows how to populate an array, setting all elements to a specific value.Theend parameter does not have to be specified.

js
const tempGirls = Array(5).fill("girl", 0);

Note that the array was initially asparse array with no assigned indices.fill() is still able to fill this array.

Calling fill() on non-array objects

Thefill() method reads thelength property ofthis and sets the value of each integer-keyed property fromstart toend.

js
const arrayLike = { length: 2 };console.log(Array.prototype.fill.call(arrayLike, 1));// { '0': 1, '1': 1, length: 2 }

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-array.prototype.fill

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp