English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Array fill() Method

 JavaScript Array Object

The fill() method uses a static value to fill all elements of the array from the start index to the end index.

Syntax:

array.fill(value, start, end)
var nums = [1, 2, 3, 4]);
nums.fill(17);
Test See‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the fill() method:

Method
fill()4531Yes8Yes

Parameter value

ParameterDescription
value(Required)Value to fill the array with
start(Optional)Index to start filling the array. The default is 0
end(Optional)Index to stop filling the array. The default value isthis.length

Technical details

Return value:Modified array
JavaScript version:ECMAScript 6

More examples

From position2To position4Fill 0:

var nums = [1, 2, 3, 4]);
nums.fill(0, 2, 4);
Test See‹/›

From position1Fill7:

var nums = [1, 2, 3, 4]);
nums.fill(7, 1);
Test See‹/›

 JavaScript Array Object