Skip to content

How to remove an element from an array in JavaScript?

Find out how to remove a particular element from an array in JavaScript. We can use the Array.splice() method to modify the contents of an array.

The splice() method in JavaScript modifies the contents of an array by removing or replacing existing elements and/or adding new elements in place.

Syntax

array.splice(startIndex,deleteCount);

The Array.splice() method takes two arguments. The position from which the element should be removed and the count of elements to be removed.

Removing an element from array

const arr = ['We','Poop','Code','A Lot'];


//To remove Poop from this array

arr.splice(1,1);

console.log(arr); // ["We", "Code", "A Lot"]


//To remove 'Code' and 'A Lot' from this array

arr.splice(1,2);

console.log(arr); // ["We"]
See also  How to clone an array in JavaScript?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.