Remove Element from Array at any Index using splice() Method

Splice method is used to modify the contents of an array by removing the existing elements and/or adding new elements. To remove elements by the splice() method you can specify the elements in different ways. 

Example 1: This example uses the indexing of the splice method to remove elements from a JavaScript array. 

javascript
// JavaScript code to illustrate splice() function 

let arr = ["shift", "splice", "filter", "pop"];

// Removing the specified element from the array 
let spliced = arr.splice(1, 1);
console.log("Removed element: " + spliced);
console.log("Remaining elements: " + arr);

Output
Removed element: splice
Remaining elements: shift,filter,pop

Example 2: This example uses the value of the splice method to remove elements from a JavaScript array. 

javascript
// JavaScript code to illustrate splice() function 
let arr = ["shift", "splice", "filter", "pop"];

// Removing the specified element by value from the array 
for (let i = 0; i < arr.length; i++) {
    if (arr[i] === "splice") {
        let spliced = arr.splice(i, 1);
        console.log("Removed element: " + spliced);
        console.log("Remaining elements: " + arr);
    }
}

Output
Removed element: splice
Remaining elements: shift,filter,pop

Example 3: Using the splice method to remove each element from a JavaScript array. 

javascript
// Declare and initialize array
let array = ["pop", "splice", "filter", "shift"]

console.log("Original array: " + array)

// Making the length of array to 0 by using splice method
array.splice(0, array.length);
console.log("Empty array: " + array)

Output
Original array: pop,splice,filter,shift
Empty array: 

Remove elements from a JavaScript Array

Removing elements from a JavaScript array means taking out certain items from the array. This is a basic and important task in programming because it helps keep the array accurate and makes data handling easier. In this article, we will discuss few methods to remove elements from a JavaScript Array.

Similar Reads

Methods to Remove Elements from JavaScript Array

There are different methods to remove elements from a JavaScript array which are discussed below:...

Method 1: Remove Last Element form Array using pop() Method

This method is used to remove the last element of the array and returns the removed element. This function decreases the length of the array by 1 every time the element is removed....

Method 2: Remove First Element from Array using shift() Method

This method is used to remove and return the first element of the array and reduce the size of the original array by 1....

Method 3: Remove Element from Array at any Index using splice() Method

Splice method is used to modify the contents of an array by removing the existing elements and/or adding new elements. To remove elements by the splice() method you can specify the elements in different ways....

Method 4: Remove Element from Array with Certain Condition using filter() Method

Filter method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument function. To remove elements by filter() method you can specify the elements in different ways....

Method 5: Remove Array Elements with Index using Delete Operator

The delete operator returns a boolean value, true, if the element or property is removed from the array or object and false, if a function or variable is passed to remove....

Method 6: Remove Array Elements using Clear and Reset Approach

Removing elements from an array using the manual clear and reset approach either by resetting the length of the array as 0 using the length property or by assigning the array to an empty array([])....

Method 7: Remove Element from Array using for() loop and new array

Here a simple for loop will be run over the array and pushes all elements in the new array except the element that has to be removed....

Method 8: Remove Array Element using lodash _.remove Method

Use the lodash library to remove elements from a JavaScript array. To use lodash library you need to install it locally on your system....