JavaScript Array.splice() Method
The splice() method is used to Insert and Remove elements in between the Array.
Syntax
Array.splice (start, deleteCount, item 1, item 2….)
Example : Removing an element and adding new elements at the same time using the splice() method.
// Declare and initialize array
let numArr = [20, 30, 40, 50];
// Removing an adding element at a
// particular location in an array
// Delete 3 elements starting from index 1
numArr.splice(1, 3);
// Insert 80, 90, 100 at starting index 1
numArr.splice(1, 0, 3, 4, 5);
console.log(numArr);
// Declare and initialize array
let strArr = ["amit", "sumit", "anil"];
// Delete two elements starting from index 1
// and add three elements.
strArr.splice(1, 2, "Geeks", "Geeks1", "Geeks2");
Output
[ 20, 3, 4, 5 ]
JavaScript Array Methods
JavaScript array methods are built-in functions that allow efficient manipulation and traversal of arrays. They provide essential functionalities like adding, removing, and transforming elements, as well as searching, sorting, and iterating through array elements, enhancing code readability and productivity.
Learn More on JavaScript Array
Now let’s look at the JS array methods and properties.