• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • Array.prototype.fill()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)
      • 正體中文 (繁體)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. Array.from()
    2. Array[Symbol.species]
  • Instance methods
    1. Array.prototype.entries()
    2. Array.prototype.find()
    3. Array.prototype.flat()
    4. Array.prototype.indexOf()
    5. Array.prototype.map()
    6. Array.prototype.reduceRight()
    7. Array.prototype.some()
    8. Array.prototype.toReversed()
    9. Array.prototype.unshift()
    10. Array: length
    11. Function.prototype.bind()
    12. Function: displayName Non-standard
    13. Function.prototype.arguments Non-standard Deprecated
    14. Function.prototype.caller Non-standard Deprecated
  • Instance methods
    1. Object.prototype.__defineGetter__() Deprecated
    2. Object.prototype.__defineSetter__() Deprecated
    3. Object.prototype.__lookupGetter__() Deprecated
    4. Object.prototype.__lookupSetter__() Deprecated
    5. Object.prototype.toLocaleString()
    6. Object.prototype.__proto__ Deprecated
    7. Array 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 4
    console.log(array1.fill(0, 2, 4));
    / Expected output: Array [1, 2, 0, 0]
    
    / Fill with 5 from position 1
    console.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: if value is an object, each slot in the array will reference that object.

    start Optional

    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.
    • If start < -array.length or start is omitted, 0 is used.
    • If start >= array.length, no index is filled.
    end Optional

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

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

    Return value

    The modified array, filled with value.

    Description

    The fill() method is a mutating method. It does not alter the length of this, but it will change the content of this.

    The fill() method fills empty slots in sparse arrays with value as well.

    The fill() method is generic. It only expects the this value to have a length property. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

    Note: Using Array.prototype.fill() on an empty array (length = 0) would not modify it as the array has nothing to be modified. To use Array.prototype.fill() when declaring an array, make sure the array has non-zero length. 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 the ones() 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]); / 10
    console.log(arr[1][0]); / 1
    console.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. The end parameter does not have to be specified.

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

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

    Calling fill() on non-array objects

    The fill() method reads the length property of this and sets the value of each integer-keyed property from start to end.

    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

    • Polyfill of Array.prototype.fill in core-js
    • Indexed collections guide
    • Array
    • TypedArray.prototype.fill()

    Help improve MDN

    fill() method of arrays and typed arrays sets all or some items of an array to a given a value.","name":"Array fill()"}},"browserCompat":["javascript.builtins.Array.fill"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant