How to remove an item from an array in Javascript

The standard way to remove an item from an array is to use indexOf (opens new window) to find the position of the item and then use splice (opens new window) to remove the item from the array.

Copy code
      
    


And this works just fine if you know your array doesn't contain duplicates or you only want to remove a single instance. If your array contains duplicates and you want to remove every instance of an item, then you'll want to use a while loop to iterate over the items and then use splice (opens new window) to remove the item.

Copy code
      
    

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.