JavaScript Arrays Remove value from array

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

When you need to remove a specific value from an array, you can use the following one-liner to create a copy array without the given value:

array.filter(function(val) { return val !== to_remove; });

Or if you want to change the array itself without creating a copy (for example if you write a function that get an array as a function and manipulates it) you can use this snippet:

while(index = array.indexOf(3) !== -1) { array.splice(index, 1); }

And if you need to remove just the first value found, remove the while loop:

var index = array.indexOf(to_remove);
if(index !== -1) { array.splice(index , 1); }


Got any JavaScript Question?