OneBite.Dev - Coding blog in a bite size

remove item from array in Javascript

Code snippet on how to remove item from array in Javascript

  let array = ["a", "b", "c"];
  let itemToRemove = "b";

  let newArray = array.filter(el => el !== itemToRemove);

  // result
  console.log(newArray); // ["a", "c"]

This code shows how to remove an item from an array in JavaScript. The first line provides the array, with 3 elements: “a”, “b”, and “c”. The second line defines the item to remove from the array, which is “b”. The third line creates a new array using the .filter() function. The .filter() function allows us to filter out any elements that do not match the condition given. In this case, if the element does not equal the itemToRemove, it is included in the newArray. Finally, when the code is executed, the console logs the newArray, with the elements “a” and “c”, excluding the element “b”.

javascript