WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to Remove an Item from a JavaScript Array by Value

Topic: JavaScript / jQueryPrev|Next

Answer: Use the indexOf() and splice() Methods

You can simply use the JavaScript indexOf() method in combination with the splice() method to remove an item or element from an array by value.

The indexOf() method returns the index of the first occurrence of the specified element in the array, or -1 if it is not present, whereas splice() method used to add or remove elements at any index.

Let's take a look at the following example to understand how it basically works:

// Sample array
var array = ["red", "green", "blue"];

// Item to remove
var item = "green";

// Get the index of the item
var index = array.indexOf(item);

// Check to see if the item exists in the array, if exists then remove it
if(index !== -1) {
    array.splice(index, 1);
}

console.log(JSON.stringify(array)); // Prints: ["red","blue"]

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties