How to Remove Element from Array at Specific Index Javascript

Last Updated on April 14, 2024 by Roshan Parihar

To remove specific element from an array in Javascript, you can use the splice() function with its argument as the index position of the specified array element.

The filter() function can also be useful to remove the specified element of an array in javascript. Let’s find out with the different examples given below.

Remove Specific Element From Array Using indexOf() and splice() Functions in Javascript

It first finds the index position of the specified element using the indexOf() function. If the index position is positive, it means the element is matching with the specified element.

After that, use the array.splice() function to delete the matching element from the array.

Output

Remove Specific Element From Array Using indexOf() and splice() Functions in Javascript

The specified element is 7 to remove from the array. To remove it, you have to click the button given above. It gives you output that is an array without the specified array element to remove.

Delete Specified Array Element Using filter() Function in Javascript

If you want to remove the specified element from an array, use only the filter() function. It requires only to return the specified element to delete from the array. See the example given below to learn the method.

Output


The specified element is 21 that you have to remove from the given array. The above example contains the button and you can click it to delete the specified element from the array.

Using Javascript For Loop with If Condition and splice()

In addition to the above methods, you can also use the combination of for loop and if condition to remove array elements in javascript. Firstly, find the length of the array to perform iteration over the given array according to the size.

The iteration is required to check if each element if matching with the specified element in each loop. When the specified element matching with the array element, use the splice() and pass the index position to remove the element from the array.

Output


The given element is 13 to remove from the array using javascript. Now, click the button given above to delete the specified element from the given array. After you click the button, you will get an output that shows the item is removed from the array.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.