Array.prototype.splice()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/splice
  • Revision title: Array splice method
  • Revision id: 321749
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

Summary

Changes the content of an array, adding new elements while removing old elements.

Method of Array instances
Implemented in JavaScript 1.2
ECMAScript Edition ECMAScript 3rd Edition

Syntax

array.splice(index , howMany[, element1[, ...[, elementN]]])

array.splice(index[, howMany[, element1[, ...[, elementN]]]])

Parameters

index
Index at which to start changing the array. If negative, will begin that many elements from the end.
howMany
An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.
element1, ..., elementN
The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array.

Returns

An array containing the removed elements. If only one element is removed, an array of one element is returned.

Description

If you specify a different number of elements to insert than the number you're removing, the array will have a different length at the end of the call.

Backward Compatibility

JavaScript 1.2

The splice method returns the element removed, if only one element is removed (howMany parameter is 1); otherwise, the method returns an array containing the removed elements. Note that the last browser to use JavaScript 1.2 was Netscape Navigator 4, so you can depend on splice always returning an array.

Examples

Example: Using splice

The following script illustrate the use of splice:

var myFish = ["angel", "clown", "mandarin", "surgeon"];

//removes 0 elements from index 2, and inserts "drum"
var removed = myFish.splice(2, 0, "drum");
//myFish is ["angel", "clown", "drum", "mandarin", "surgeon"]
//removed is [], no elements removed

//removes 1 element from index 3
removed = myFish.splice(3, 1);
//myFish is ["angel", "clown", "drum", "surgeon"]
//removed is ["mandarin"]

//removes 1 element from index 2, and inserts "trumpet"
removed = myFish.splice(2, 1, "trumpet");
//myFish is ["angel", "clown", "trumpet", "surgeon"]
//removed is ["drum"]

//removes 2 elements from index 0, and inserts "parrot", "anemone" and "blue"
removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
//myFish is ["parrot", "anemone", "blue", "trumpet", "surgeon"]
//removed is ["angel", "clown"]

See also

  • push / pop - add/remove elements from the end of the array;
  • shift / unshift - add/remove elements from the beginning of the array;
  • concat - returns a new array comprised of this array joined with other array(s) and/or value(s).

Revision Source

<h2 id="Summary">Summary</h2>
<p>Changes the content of an array, adding new elements while removing old elements.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Array#Array_instances"><code>Array</code> instances</a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.2</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>array</em>.splice(<em>index</em> ,<em> howMany</em>[<em>,</em><em> element1</em>[, ...[, <em>elementN</em>]]])</code>

<code><em>array</em>.splice(<em>index</em>[,<em> howMany</em>[,<em> element1</em>[, ...[, <em>elementN</em>]]]])</code></pre>
<h3 id="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>index</code></dt>
  <dd>
    Index at which to start changing the array. If negative, will begin that many elements from the end.</dd>
</dl>
<dl>
  <dt>
    <code>howMany</code></dt>
  <dd>
    An integer indicating the number of old array elements to remove. If <code>howMany</code> is 0, no elements are removed. In this case, you should specify at least one new element. If no <code>howMany</code> parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after <code>index</code> are removed.</dd>
</dl>
<dl>
  <dt>
    <code>element1, ..., element<em>N</em></code></dt>
  <dd>
    The elements to add to the array. If you don't specify any elements, <code>splice</code> simply removes elements from the array.</dd>
</dl>
<h2 id="Returns">Returns</h2>
<p>An array containing the removed elements. If only one element is removed, an array of one element is returned.</p>
<h2 id="Description">Description</h2>
<p>If you specify a different number of elements to insert than the number you're removing, the array will have a different length at the end of the call.</p>
<h2 id="Backward_Compatibility">Backward Compatibility</h2>
<h3 id="JavaScript_1.2">JavaScript 1.2</h3>
<p>The <code>splice</code> method returns the element removed, if only one element is removed (<code>howMany</code> parameter is 1); otherwise, the method returns an array containing the removed elements. Note that the last browser to use JavaScript 1.2 was Netscape Navigator 4, so you can depend on <code>splice</code> always returning an array.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Example.3A_Using_splice">Example: Using <code>splice</code></h3>
<p>The following script illustrate the use of splice:</p>
<pre class="brush: js">
var myFish = ["angel", "clown", "mandarin", "surgeon"];

//removes 0 elements from index 2, and inserts "drum"
var removed = myFish.splice(2, 0, "drum");
//myFish is ["angel", "clown", "drum", "mandarin", "surgeon"]
//removed is [], no elements removed

//removes 1 element from index 3
removed = myFish.splice(3, 1);
//myFish is ["angel", "clown", "drum", "surgeon"]
//removed is ["mandarin"]

//removes 1 element from index 2, and inserts "trumpet"
removed = myFish.splice(2, 1, "trumpet");
//myFish is ["angel", "clown", "trumpet", "surgeon"]
//removed is ["drum"]

//removes 2 elements from index 0, and inserts "parrot", "anemone" and "blue"
removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
//myFish is ["parrot", "anemone", "blue", "trumpet", "surgeon"]
//removed is ["angel", "clown"]</pre>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/push" title="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/push">push</a> / <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/pop" title="en/JavaScript/Reference/Global_Objects/Array/pop">pop</a> - add/remove elements from the end of the array;</li>
  <li><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/shift" title="en/JavaScript/Reference/Global_Objects/Array/shift">shift</a> / <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift" title="en/JavaScript/Reference/Global_Objects/Array/unshift">unshift</a> - add/remove elements from the beginning of the array;</li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat" title="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/splice">concat</a> - returns a new array comprised of this array joined with other array(s) and/or value(s).</li>
</ul>
Revert to this revision