JavaScript Array splice() Method
Last Updated :
05 Sep, 2024
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.
Syntax
Array.splice( index, remove_count, item_list )
Parameters:
- 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.
Example 1: Basic Usage of splice() Method
In this example, JS is replaced with “PHP” and React_Native, and React is inserted at the second-to-last index.
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' ]
Example 2: Manipulating an Array with splice() Method
In this example we demonstrates modifying the languages array. It first replaces ‘Html’ with ‘Julia’ and ‘Php,’ then inserts ‘Pascal’ before the second-to-last element, logging the changes throughout.
JavaScript
let languages = ['C++', 'Java', 'Html', 'Python', 'C'];
console.log(languages);
// Replace 'Html' with 'Julia' and 'Php'.
let removed = languages.splice(2, 1, 'Julia', 'Php');
console.log(languages);
console.log(removed);
// Insert 'Pascal' before the second-to-last element.
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'
]
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
JavaScript Array splice() Method- FAQs
How do you use the splice() method?
The splice() method is used with the following syntax:
array.splice(start, deleteCount, item1, item2, …);
Can you use splice() to only add elements to an array without removing any?
Yes, by omitting the items to add, you can remove elements without adding any.
What happens if the start index is negative?
If the start index is greater than the array length, splice() will start adding the new elements at the end of the array without removing any elements.
What if the deleteCount parameter is not specified?
If the deleteCount parameter is not specified, all elements from the start index to the end of the array are removed.
Can splice() be used on non-array objects?
While splice() is a method of array objects, it can be called on array-like objects using Function.prototype.call() or Function.prototype.apply().
Similar Reads
JavaScript Array() Constructor
The Array() constructor is used to create Array objects and the array constructor can be called with or without a new keyword, both can create a new Array. Syntax:new Array(Value1, Value2, ...);new Array(ArrayLength);Array(Value1, Value2, ...);Array(ArrayLength);Parameters: ValueN: An array initiali
2 min read
JavaScript Array constructor Property
The JavaScript Array constructor property is used to return the constructor function for an array object. It only returns the reference of the function and does not return the name of the function. In JavaScript arrays, it returns the function Array(){ [native code] }. Syntax: array.constructorRetur
2 min read
JavaScript Array length
JavaScript array length property is used to set or return the number of elements in an array. [GFGTABS] JavaScript let a = ["js", "html", "gfg"]; console.log(a.length); [/GFGTABS]Output3 Setting the Length of an ArrayThe length property can also be used to set the lengt
2 min read
JavaScript Array from() Method
The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object. Syntax : Array.from(object, mapFunction, thisValue)Parameters:object: This Parameter is required to specify the object to convert to an array.mapFunction: This Parameter specifies
3 min read
JavaScript Array isArray() Method
The isArray() method in JavaScript is used to determine whether a given value is an array or not. This method returns true if the argument passed is an array else it returns false. Syntax:Array.isArray(obj);Parameters:obj: This parameter holds the object that will be tested.Return value:This functio
3 min read
JavaScript Array of() Method
The Javascript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method. Syntax:Array.of(element0, element1, ....)Parameters: Parameters present are element0, element1, .... which are basically an element for which th
2 min read
Javascript Array at() Method
The JavaScript Array at() method takes an integer value (index) as a parameter and returns the element of that index. It allows positive and negative integers. For the negative integer, it counts back from the last element in the array. Syntax: at(index);Parameter: This method accepts one parameter
3 min read
JavaScript Array concat() Method
The concat() method concatenates (joins) two or more arrays. It returns a new array, containing the joined arrays. This method is useful for combining arrays without modifying the originals. Syntax:let newArray1 = oldArray.concat()let newArray2 = oldArray.concat(value0)let newArray3 = oldArray.conca
3 min read
JavaScript Array copyWithin() Method
The Javascript Array.copyWithin() method considers an array first and then copies part of an array to the same array itself and returns it, without modifying its size but yet the modified data whatever user wishes to have in another's place i.e, copies array element of an array within the same array
3 min read
JavaScript Array entries() Method
The entries() method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array. It allows iterating over arrays and accessing both the index and value of each element sequentially. Syntax:array.entries()Parameters:This method does not accept any parameters.
3 min read