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 and 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)The value to fill the array with
start(Optional)The index to start filling the array. The default is 0
end(Optional)The 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 and See‹/›

From position1Fill7:

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

 JavaScript Array Object