Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. Array .splice()

Array .splice()

Since: ES3(ECMAScript 1999)

A method that adds, removes, or replaces elements at any position in an array. It modifies the original array directly.

Syntax

// Removes elements starting at the specified index.
var removed = array.splice(startIndex, deleteCount);

// Inserts elements at the specified index without removing any.
array.splice(startIndex, 0, item1, item2, ...);

// Replaces elements at the specified index.
array.splice(startIndex, deleteCount, item1, item2, ...);

Arguments

ArgumentDescription
startIndexThe index at which to start the operation. A negative value counts from the end of the array.
deleteCountThe number of elements to remove. Specify 0 to insert without removing. If omitted, all elements from startIndex to the end are removed.
itemsOptional. Elements to insert at the start position. Multiple elements can be specified as comma-separated values.

Sample Code

sample_splice.js
// Pattern 1: Removing elements
var members = ["Kiryu Kazuma", "Majima Goro", "Akiyama Shun", "Nishikiyama Akira", "Saejima Taiga"];

// Removes 2 elements starting at index 1.
var removed = members.splice(1, 2);
console.log(removed); // Outputs "Majima Goro,Akiyama Shun".
console.log(members); // Outputs "Kiryu Kazuma,Nishikiyama Akira,Saejima Taiga".

// Pattern 2: Inserting elements
// Inserts elements at index 1 without removing any.
members.splice(1, 0, "Majima Goro", "Akiyama Shun");
console.log(members); // Outputs "Kiryu Kazuma,Majima Goro,Akiyama Shun,Nishikiyama Akira,Saejima Taiga".

// Pattern 3: Replacing elements
// Replaces the element at index 2.
members.splice(2, 1, "Kiryu Kazuma");
console.log(members); // Outputs "Kiryu Kazuma,Majima Goro,Kiryu Kazuma,Nishikiyama Akira,Saejima Taiga".

// Pattern 4: Using a negative index to count from the end
var roster = ["Kiryu Kazuma", "Majima Goro", "Akiyama Shun", "Nishikiyama Akira", "Saejima Taiga"];
// Removes the second-to-last element (Nishikiyama Akira).
var popped = roster.splice(-2, 1);
console.log(popped); // Outputs "Nishikiyama Akira".
console.log(roster); // Outputs "Kiryu Kazuma,Majima Goro,Akiyama Shun,Saejima Taiga".

// Pattern 5: Practical use — find and replace a specific element
var enemies = ["Kurita", "Shimano", "Kiryu Kazuma", "Omi Alliance"];
// Find the index of "Kiryu Kazuma" and replace it.
var idx = enemies.indexOf("Kiryu Kazuma");
if (idx !== -1) {
	enemies.splice(idx, 1, "Nishikiyama Akira");
}
console.log(enemies); // Outputs "Kurita,Shimano,Nishikiyama Akira,Omi Alliance".
[ 'Majima Goro', 'Akiyama Shun' ]
[ 'Kiryu Kazuma', 'Nishikiyama Akira', 'Saejima Taiga' ]
[ 'Kiryu Kazuma', 'Majima Goro', 'Akiyama Shun', 'Nishikiyama Akira', 'Saejima Taiga' ]
[ 'Kiryu Kazuma', 'Majima Goro', 'Kiryu Kazuma', 'Nishikiyama Akira', 'Saejima Taiga' ]
[ 'Nishikiyama Akira' ]
[ 'Kiryu Kazuma', 'Majima Goro', 'Akiyama Shun', 'Saejima Taiga' ]
[ 'Kurita', 'Shimano', 'Nishikiyama Akira', 'Omi Alliance' ]

Description

array.splice() is a versatile method that can remove, insert, and replace elements at any position in an array in a single call. It is a mutating method that modifies the original array in place, and its return value is an array of the removed elements. If no elements were removed, an empty array is returned.

If you specify a negative value for the start index, it is treated as an offset from the end of the array. For example, -1 refers to the last element, and -2 refers to the second-to-last element. If you omit the second argument, all elements from the start index to the end are removed, which is convenient when you want to truncate the end of an array.

If you want to extract a portion of an array without modifying the original, use array.slice() instead. The names look similar, but there is an important difference: array.splice() modifies the original array, while array.slice() does not.

Browser Compatibility

Chrome Chrome
49+
Firefox Firefox
57+
Safari Safari
18+
Edge Edge
80+
11 or earlier ×
IE IE
11+
4.5 or earlier ×
Opera Opera
48+
3 or earlier ×
iOS Safari iOS Safari
18+
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .