JavaScript Splice

Back to home
Logicmojo - Updated Aug 28, 2021



Javascript Splice

For JavaScript Array objects, the splice() method is a built-in method. It allows you to update the array's content by removing or replacing current elements with new ones.

This function alters the original array and returns a new array with the removed elements.

In this article, you'll learn how to use the splice() method to delete, add, or replace elements of an array. Let's begin by deleting elements from an array.

Below is the syntax for splice:

arr.splice(start, deleteCount[optional], newElem[optional], newElem [optional], ...);

Parameters

⮞ The start argument defines the index at which an element should be inserted or removed.

⮞ The second input provides the total number of elements to be deleted, beginning at the specified start index.

⮞ The element you want to add is specified by the third parameter.

⮞ The first argument is required, while the rest are optional. If you have a lot of elements to add to the array, you can add more additional parameters to the third argument.

var store = ['pen', 'paper', 'stone', 'pencil'];
var removed = store.splice(2, 0, 'drum');
// myFish is ["pen", "paper", "drum", "stone", "pencil"] 
// removed is [], no elements removed



let god = ['Ram', 'Shiva'];
god.splice(0,0,"Jesus"); //returns removed array: []
console.log(god); // Original array is mutated.
//["Jesus", "Ram", "Shiva"]



Jesus was inserted to the array at index 0 in the example above. If I wanted to make the splice procedure above into a literal phrase, I would remove 0 elements at index 0 and add Jesus. It's also interesting to note that the original array has been altered.

Here's another example where we added two components to the second index while removing one:

let god = ['Jesus', 'Ram', "Shiva"];
god.splice(2,1,"Ganesh","Indra"); //returns removed array:["Shiva"]
console.log(god); // Original array is mutated.
//returns: ["Jesus", "Chris", "Ganesh", "Indra"]



We eliminated Shiva and replaced him with Ganesh and Indra in the example above. The splice method returns the removed element in a new array because we removed an element(Alex).

Slice

slice() is an Array Object and String Object function. It is non-destructive because it returns a new copy and does not affect the input array's content. To avoid side effects, slice() is commonly used in function programming.

The slice() method copies a small section of an array into a new array object, starting at the beginning and ending at the end ( end is not included ). There will be no changes to the original array.

Differences between slice and splice

The slice method:

SliceSplice
Slice does not mutate the original array.Splice Mutates the original array
Takes two arguments.It has one mandate argument and can take a couple of optional ones to replace elements.
Slice makes a shallow copy of the original array.It mutates the original array by adding or removing the elements.
Returns the copied array.Returns removed elements in a new array.



Conclusion: At this point, you should have a better understanding of JavaScript Splice(). When you need to delete or add additional elements to an array, you usually utilise the splice() method. You may also use it to separate an array with mixed content in some cases, like in the example above.