How to remove a particular element from an array in Javascript?

Information:

To remove a particular element from an array we will use 2 important built-in methods of javascript. You can learn more about these two methods by clicking on its name.

  1. indexOf()
  2. slice()

Remove a particular element from an array :


// Remove 2 from the given array
var array = [1, 2, 3];

var index = array.indexOf(2); //index of 2 is 1
if (index > -1) { //1 > -1
  array.splice(index, 1); //remove one element from index 1 which is 2
}

console.log(array);
// output: [1, 3]

Leave a Reply

avatar
  Subscribe  
Notify of