How to Removed a Element from an Array in JavaScript ?

Today i am going to discuss how to removed a particular element from the Array in JavaScript. For doing this you need to find the index of specific element which you want to remove from the array, after finding the index you can easily delete that element from the array. JavaScript provides two predefined function splice & indexof by using its you can perform the task.

Splice method is used to adds/removes items from an array and return array containing removed items. This function change the original array. here are cool examples JavaScript method splice.

Example 1 : First find the index of element you need to remove.

var array = [6, 9, 4,3];
var indx = array.indexOf(4);

array.indexof method return the index of element 4 then use splice method to removed the particular element from the array.

array.splice(index, 1);

it will removed one item to given index number and the Output is 6 9 3.

Example 2: At position 2 add two new items and removed 1 items.

var countries = [“Pakistan”, “USA”, “England”, “New Zealand “];
countries.splice(2, 1, “Iran”, “Poland”);

Result: Pakistan, USA, New Zealand, Iran, Poland.

Splice JavaScript method supports almost all browsers (Mozilla Firefox, Chome, Internet Explorer etc ).  Splice method need 3 parameters first two parameters are required and last one parameter are optional.

First parameter is an Index of element where you want to adds or removes items.
Second parameter is used to tell how many number of items to be removed.
Last parameter is optional:  Number of new item(s) to be added in an array.

About Moazzam Qureshi

I am SEO expert and web developer since 2011. My hobby is to write & provides quality content about blogging, SEO , Social Media , Earn Online and Affiliate Marketing.

Leave a Reply