Open In App

JavaScript Array splice() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

JavaScript Array splice() Method is an inbuilt method in JavaScript that is used to change the contents of an array by removing or replacing existing elements and/or adding new elements. It modifies the original array and returns an array of the removed elements.

Array splice() Syntax:

Array.splice( index, remove_count, item_list )
  • index: It is a required parameter. This parameter is the index from which the modification of the array starts (with the origin at 0). This can be negative also, which begins after many elements counting from the end.
  • remove_count: The number of elements to be removed from the starting index.
  • items_list: The list of new items separated by a comma operator that is to be inserted from the starting index.

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.

Array splice() Example

Here is the basic example of the Array splice() method.

Javascript




let webDvlop = ["HTML", "CSS", "JS", "Bootstrap"];
 
console.log(webDvlop);
 
// Add 'React_Native' and 'Php' after removing 'JS'.
let removed = webDvlop.splice(2, 1, 'PHP', 'React_Native')
 
console.log(webDvlop);
console.log(removed);
 
// No Removing only Insertion from 2nd
// index from the ending
webDvlop.splice(-2, 0, 'React')
console.log(webDvlop)


Output

[ 'HTML', 'CSS', 'JS', 'Bootstrap' ]
[ 'HTML', 'CSS', 'PHP', 'React_Native', 'Bootstrap' ]
[ 'JS' ]
[ 'HTML', 'CSS', 'PHP', 'React', 'React_Native', 'Bootstrap' ]


Explanation:

This JavaScript code demonstrates manipulating an array `webDvlop`. Initially, it logs the array contents. Then, it removes “JS” from index 2 and inserts “PHP” and “React_Native” in its place, logging the modified array and the removed element. Lastly, it inserts “React” at the second-to-last index, logging the final array.

Array splice() Example

Here is another example of the Array splice() method.

Javascript




let languages = ['C++', 'Java', 'Html', 'Python', 'C'];
 
console.log(languages);
 
// Add 'Julia' and 'Php' after removing 'Html'.
let removed = languages.splice(2, 1, 'Julia', 'Php')
 
console.log(languages);
console.log(removed);
 
// No Removing only Insertion from 2nd index from the ending
languages.splice(-2, 0, 'Pascal')
console.log(languages)


Output

[ 'C++', 'Java', 'Html', 'Python', 'C' ]
[ 'C++', 'Java', 'Julia', 'Php', 'Python', 'C' ]
[ 'Html' ]
[
  'C++',    'Java',
  'Julia',  'Php',
  'Pascal', 'Python',
  'C'
]


Explanation:

This JavaScript code operates on an array `languages`. Initially, it logs the array contents. Then, it removes “Html” from index 2 and inserts “Julia” and “Php” in its place, logging the modified array and the removed element. Lastly, it inserts “Pascal” at the second-to-last index, logging the final array.

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

Supported Browsers:



Last Updated : 05 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads