Categories
JavaScript Answers

How to remove a specific item from an array with JavaScript?

Spread the love

To remove a specific item from an array with JavaScript, we call the array splice method.

For instance, we write

const array = [2, 5, 9];
const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1);
}

console.log(array);

to get the index of 5 in array with indexOf.

Then we call splice with index and 1 to remove 5 from the array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.