Delete an Element from Array using JavaScript splice() Method

The JavaScript Array splice() Method is an inbuilt method in JavaScript that is used to modify the contents of an array by removing the existing elements and/or by adding new elements.

Example:

Javascript




function func() {
    let array = ["HTML", "CSS", "JS", "Bootstrap"];
  
    console.log("Original array: " + array);
                  
    // Add 'React' and 'Php' after removing 'JS'.
    let removedElement = array.splice(1, 1, 'PHP', 'React')
                  
    console.log("Array after splice: " + array);
    console.log("Removed element: " + removedElement);                           
}
func();


Output

Original array: HTML,CSS,JS,Bootstrap
Array after splice: HTML,PHP,React,JS,Bootstrap
Removed element: CSS

How to delete an element from an array using JavaScript ?

The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index.

In this article, we will discuss different ways to remove elements from the array. There are many methods that are used to remove elements from the JavaScript array which are discussed below.

We can delete an element or item from an array using two functions which is:

  • JavaScript Array shift() Method
  • JavaScript Array pop() Method
  • Using JavaScript splice() Method
  • Using JavaScript Array filter() Method

Similar Reads

Remove Array elements by using Array shift() Method

This method is used to delete one or more elements from the array and these elements get deleted from the beginning of the array because of the deleted elements or items from the array, the length of the array also gets decreased by the number of elements deleted from the array....

Remove elements by using the JavaScript array pop() Method

...

Delete an Element from Array using JavaScript splice() Method

...

Delete an Array Element using JavaScript Array filter() Method

This method is used to pop or delete elements or items from an array. We can pop one or more elements from the array and these elements get deleted from the end of the array because of the popped elements from the array, the length of the array also gets decreased by the number of elements popped from the array....