★ wanayoo — archive 1999 https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Global_Objects/Array/fillNouvelle recherche | Portail wanayoo

Join MDN and developers like you at Mozilla's View Source conference, November 2-4 in Portland, Oregon. Learn more at https://viewsourceconf.org/.

Our volunteers haven't translated this article into Tiếng Việt yet. Join us and help get the job done!

This is a new technology, part of the ECMAScript 2015 (ES6) standard.
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.

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

Syntax

arr.fill(value[, start = 0[, end = this.length]])

Parameters

value
Value to fill an array.
start
Optional. Start index.
end
Optional. End index.

Description

The elements interval to fill is [start, end).

The fill method takes up to three arguments value, start and end. The start and end arguments are optional with default values of 0 and the length of the this object.

If start is negative, it is treated as length+start where length is the length of the array. If end is negative, it is treated as length+end.

The fill function is intentionally generic, it does not require that its this value be an Array object.

The fill method is a mutable method, it will change this object itself, and return it, not just return a copy of it.

Examples

[1, 2, 3].fill(4);               // [4, 4, 4]
[1, 2, 3].fill(4, 1);            // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2);         // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1);         // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2);       // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN);     // [1, 2, 3]
Array(3).fill(4);                // [4, 4, 4]
[].fill.call({ length: 3 }, 4);  // {0: 4, 1: 4, 2: 4, length: 3}

Polyfill

if (!Array.prototype.fill) {
  Array.prototype.fill = function(value) {

    // Steps 1-2.
    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    var O = Object(this);

    // Steps 3-5.
    var len = O.length >>> 0;

    // Steps 6-7.
    var start = arguments[1];
    var relativeStart = start >> 0;

    // Step 8.
    var k = relativeStart < 0 ?
      Math.max(len + relativeStart, 0) :
      Math.min(relativeStart, len);

    // Steps 9-10.
    var end = arguments[2];
    var relativeEnd = end === undefined ?
      len : end >> 0;

    // Step 11.
    var final = relativeEnd < 0 ?
      Math.max(len + relativeEnd, 0) :
      Math.min(relativeEnd, len);

    // Step 12.
    while (k < final) {
      O[k] = value;
      k++;
    }

    // Step 13.
    return O;
  };
}

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Array.prototype.fill' in that specification.
Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 45 [1] 31 (31) Not supported Not supported 7.1
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported Not supported 31.0 (31) Not supported Not supported 8.0

[1] Starting with Chrome 36, this was available behind a preference. In chrome://flags, activate the entry “Enable Experimental JavaScript”.

See also

Hide Sidebar