JavaScript Array.splice() Method

Usage — The JavaScript Array.splice() method is used to change the contents of an array by removing existing elements and/or adding new elements.

It has the following syntax:

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Here is an example of using Array.splice():

var names = ['James', 'Cindy', 'Amanda', 'Martha', 'Alex', 'Arnold'];
names.splice(2, 0, 'Monty');

// Output — ["James", "Cindy", "Monty", "Amanda", "Martha", "Alex", "Arnold"]
console.log(names);

names.splice(3, 2);

// Output — ["James", "Cindy", "Monty", "Alex", "Arnold"]
console.log(names);

Return Value — This method returns an array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

Additional Information — This may seem obvious but 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.

Browser Support — The Array.splice() method is available in Chrome, Edge, Firefox, Internet Explorer 5.5+, Opera and Safari.

Go Directly To — usage, parameters or working examples.

Parameters

start

The start parameter is used to specify the index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array. This is a required parameter.

deleteCount

The deleteCount parameter is used to specify an integer indicating the number of old array elements to remove. This is an optional parameter.

If deleteCount is omitted, or if its value is larger than array.length - start (that is, if it is greater than the number of elements left in the array, starting at start), then all of the elements from start through the end of the array will be deleted.

If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element.

item1, item2, …

The item1, item2, ... parameters are used to specify the elements to add to the array, beginning at the start index. If you don’t specify any elements, Array.splice() will only remove elements from the array.

Working Examples

Here are some examples of using the Array.splice() method:

var names = ['James', 'Cindy', 'Amanda', 'Martha', 'Alex', 'Arnold'];
var spliced = names.splice(2, 0, 'Monty');

// Output — ["James", "Cindy", "Monty", "Amanda", "Martha", "Alex", "Arnold"]
console.log(names);

// Output — []
console.log(spliced);


var spliced = names.splice(3, 1);

// Output — ["James", "Cindy", "Monty", "Martha", "Alex", "Arnold"]
console.log(names);

// Output — ["Amanda"]
console.log(spliced);

var spliced = names.splice(2, 1, "Harry");

// Output — ["James", "Cindy", "Harry", "Martha", "Alex", "Arnold"]
console.log(names);

// Output — ["Monty"]
console.log(spliced);

var spliced = names.splice(-2, 1, "Larry");

// Output — ["James", "Cindy", "Harry", "Martha", "Larry", "Arnold"]
console.log(names);

// Output — ["Alex"]
console.log(spliced);

Further Reading

  1. You can read more about the JavaScript Array.splice() method on MDN.
  2. You can also take a look at the defeinition of JavaScript Array.splice() method in the ECMAScript 2015 specification.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top