A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-array-splice-method/ below:

JavaScript Array splice() Method - GeeksforGeeks

JavaScript Array splice() Method

Last Updated : 11 Jul, 2025

The splice() method in JavaScript is a powerful and flexible tool for modifying arrays. It allows us to add, remove, or replace elements within an array. It’s often used to update or manage array contents in various ways.

Syntax

array.splice(startIndex, deleteCount, item1, item2, ..., itemN);

Return Value: While it mutates the original array in place, still it returns the list of removed items. In case there is no removed array it returns an empty array.

The splice() method can perform different tasks depending on the parameters provided.

1. Removing Elements from an Array

The splice() method can be used to remove elements from an array. This is done by providing the startIndex and the deleteCount (the number of elements to remove).

JavaScript
let a = ["Apple", "Banana", "Cherry", "Date"];
a.splice(1, 2);

console.log(a);  

Output
[ 'Apple', 'Date' ]

In this example

2. Adding Elements to an Array

we can also use splice() to add elements to an array without removing any. To do this, set deleteCount to 0 and provide the elements to add.

JavaScript
let fruits = ["Apple", "Banana", "Cherry", "Date"];
fruits.splice(2, 0, "Mango", "Peach");

console.log(fruits);  

Output
[ 'Apple', 'Banana', 'Mango', 'Peach', 'Cherry', 'Date' ]

In this example

3. Replacing Elements in an Array

We can also use splice() to replace elements. Provide the startIndex, deleteCount, and the elements to replace the removed items.

JavaScript
let fruits = ["Apple", "Banana", "Cherry", "Date"];
fruits.splice(1, 2, "Orange", "Grape");

console.log(fruits);  

Output
[ 'Apple', 'Orange', 'Grape', 'Date' ]

In this example

4. Adding or Removing Elements from the End of the Array

Since splice() works with any position in the array, you can use it to add or remove elements from the end of the array by using a startIndex equal to the array length.

JavaScript
let fruits = ["Apple", "Banana", "Cherry", "Date"];
fruits.splice(fruits.length - 1, 1);  // Removes the last element

console.log(fruits);  

Output
[ 'Apple', 'Banana', 'Cherry' ]
5. Handling Negative Indices

We can also use negative numbers for the startIndex to remove or add elements starting from the end of the array. Negative indices count from the end, where -1 refers to the last element, -2 to the second last, and so on.

JavaScript
let fruits = ["Apple", "Banana", "Cherry", "Date"];
fruits.splice(-2, 1, "Mango");

console.log(fruits);  

Output
[ 'Apple', 'Banana', 'Mango', 'Date' ]

In this example

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Conclusion

The splice() method in JavaScript is a powerful tool for adding, removing, or replacing elements in an array. It directly modifies the original array and offers flexibility in handling array data by specifying the starting index, number of elements to remove, and new elements to add. Whether manipulating specific elements or working with the array's end, splice() simplifies array management.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4