---
title: Array.prototype.fill()
slug: Web/JavaScript/Reference/Global_Objects/Array/fill
tags:
  - Array
  - ECMAScript 2015
  - JavaScript
  - Method
  - Prototype
  - polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Array/fill
---
<div>{{JSRef}}</div>

<p>Die <code><strong>fill()</strong></code> Methode befüllt ein Array mit einem statischen Wert von einem Startindex bis zu einem Endindex. Der Endindex wird nicht mit eingeschlossen.</p>

<div>{{EmbedInteractiveExample("pages/js/array-fill.html")}}</div>



<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><code><var>arr</var>.fill(<var>value</var>[, <var>start<var>[, <var>end</var>]])</var></var></code></pre>

<h3 id="Parameter">Parameter</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>Wert, mit dem ein Array gefüllt werden soll.</dd>
 <dt><code>start</code> {{optional_inline}}</dt>
 <dd>Startindex, Standardwert ist 0.</dd>
 <dt><code>end</code> {{optional_inline}}</dt>
 <dd>Endindex, Standardwert ist <code>this.length</code>.</dd>
</dl>

<h3 id="Rückgabewert">Rückgabewert</h3>

<p>Das geänderte Array.</p>

<h2 id="Beschreibung">Beschreibung</h2>

<p>Die <strong><code>fill</code></strong> Methode nimmt bis zu drei Argumente entgegen: <code>value</code>, <code>start</code> und <code>end</code>. Die Argumente <code>start</code> und <code>end</code> sind optional und haben als Standardwert 0 und <code>length</code> des <code>this</code> Objektes.</p>

<p>Wenn <code>start</code> negativ ist, wird stattdessen <code>length+start</code> benutzt, wobei <code>length</code> die Länge des Arrays ist. Wenn <code>end</code> negativ ist, wird stattdessen <code>length+end</code> benutzt.</p>

<p>Die <code><strong>fill</strong></code> Funktion ist absichtlich generisch. Es ist nicht nötig, dass der <code>this</code> Wert ein Array Objekt ist.</p>

<p>Die <code><strong>fill</strong></code> Methode ist eine verändernde Methode. Sie verändert das <code>this</code> Objekt selbst und gibt dieses zurück. Sie erstellt keine Kopie des Objektes.</p>

<p>Wenn der <code><strong>fill</strong></code> Methode ein Objekt übergeben wird, wird das Objekt kopiert und die Referenz der Kopie wird im Array eingesetzt.</p>

<h2 id="Beispiele">Beispiele</h2>

<pre class="brush: js">[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, 3);         // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2);       // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN);     // [1, 2, 3]
[1, 2, 3].fill(4, 3, 5);         // [1, 2, 3]
Array(3).fill(4);                // [4, 4, 4]
[].fill.call({ length: 3 }, 4);  // {0: 4, 1: 4, 2: 4, length: 3}

// Objects by reference.
var arr = Array(3).fill({}) // [{}, {}, {}];
arr[0].hi = "hi"; // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]
</pre>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">if (!Array.prototype.fill) {
  Object.defineProperty(Array.prototype, 'fill', {
    value: 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 &gt;&gt;&gt; 0;

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

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

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

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

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

      // Step 13.
      return O;
    }
  });
}
</pre>

<p>Wenn es wirklich notwendig ist veraltete JavaScript-Umgebungen zu unterstützen, die <code><a href="/de/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code> nicht unterstützen, ist es meistens besser Methoden von <code>Array.prototype</code> nicht mit einem Polyfill zu unterstützen, weil sie nicht als nicht-aufzählbar eingestellt werden können.</p>

<h2 id="Spezifikationen">Spezifikationen</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Spezifikation</th>
   <th scope="col">Status</th>
   <th scope="col">Kommentar</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-array.prototype.fill', 'Array.prototype.fill')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initiale Definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.fill', 'Array.prototype.fill')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browserkompatibilität">Browserkompatibilität</h2>

<div>


<p>{{Compat("javascript.builtins.Array.fill")}}</p>
</div>

<h2 id="Siehe_auch">Siehe auch</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
 <li>{{jsxref("TypedArray.prototype.fill()")}}</li>
</ul>