How to remove an element from an array in JavaScript
How to remove an element from an array in JavaScript

Two ways to quickly remove an element from an array in JavaScript

So you want to remove an item from an array in JavaScript?

Well, you’re not alone!

It’s one of the most upvoted JavaScript questions on stack overflow and can feel a little unnatural considering how simple it is to array.push() .

Method 1 — remove an element with Array.filter()

If you want to remove an element while leaving the original array intact (unmutated) then filter() is a good choice!

Removing a single element:

const itemToRemove = 3 const originalArray = [2, 51, 3, 44] const newArray = originalArray.filter(item => item !== itemToRemove) console.log(newArray) // [2, 51, 44]

console.log(originalArray) // [2, 51, 3, 44]

Breaking that down.

We define a variable newArray and set it equal to the return value of our array filter. Inside of our filter, we pass an ECMAScript 6 arrow function.

This function tests each item in the array and returns:
['Kitson Broadhurst']
['JavaScript', 'Programming', 'Arrays', 'Technology', 'Software Engineering']