Javascript - How to remove an element from an array using splice

You can delete an item in an array using the splice function. All you need to know is the index of the element you want to remove and combine it with splice. Below is an example of this:

var array = ['one', 'two', 3, "four", 5];
array.splice(2,1);
console.log(array); //result is ['one', 'two', 'four', 5]

In the above we remove item at index 2 (0 based), which is item 3. The length of the array is changed and it leaves no empty element contrary to using delete which does not change the length. All you need to know is the index of the item you want to remove. If you want to remove by value, you can find the index using indexOf:

var array = ['one', 'two', 3, "four", 5];
var index = array.indexOf(3); // will be 2
array.splice(index, 1);
console.log(array); //result is ['one', 'two', 'four', 5]

That is all there is to it, I hope you will find the above useful! Feel free to leave a comment down below, I read them all!