In JavaScript, you can remove a specific item from an array using various methods. Here are some common ways to do so:

Using filter() method

The filter() method creates a new array with elements that pass the provided test. It doesn’t mutate the original array.

let array = [1, 2, 3, 4, 5];
const itemToRemove = 3; // Item to remove
array = array.filter(item => item !== itemToRemove);

console.log(array); // Output: [1, 2, 4, 5]

Using splice() method

The splice() method changes the contents of an array by removing or replacing existing elements. It modifies the array in place.

let array = [1, 2, 3, 4, 5];
const itemToRemove = 3; // Item to remove
const indexToRemove = array.indexOf(itemToRemove);// Index of the item to remove

array.splice(indexToRemove, 1); // Removes one element at the specified index

console.log(array); // Output: [1, 2, 4, 5]

Support On Demand!

JavaScript