There are a few ways to remove a specific item from an array in JavaScript.
Method 1: Using the indexOf() method
The indexOf() method can be used to find the index of an item in an array. Once you have the index of the item you want to remove, you can use the
splice() method to remove it from the array.
The splice() method takes three arguments:
The index of the item you want to remove
The number of items you want to remove
A new array of items to replace the removed items
For example, the following code removes the item with the index 1 from the array
myArray:
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are a few ways to remove a specific item from an array in JavaScript.
Method 1: Using the
indexOf()methodThe
indexOf()method can be used to find the index of an item in an array. Once you have the index of the item you want to remove, you can use thesplice()method to remove it from the array.The
splice()method takes three arguments:For example, the following code removes the item with the index 1 from the array
myArray:Method 2: Using the
forloopThe
forloop can be used to iterate through the array and remove the item you want to remove.The
forloop takes three arguments:For example, the following code removes the item with the value
2from the arraymyArray:Method 3: Using the
whileloopThe
whileloop can be used to iterate through the array and remove the item you want to remove.The
whileloop takes two arguments:For example, the following code removes the item with the value
2from the arraymyArray: