How to Remove a Specific Element from an Array in JavaScript: A Detailed Guide with Examples
\n\nHello, fellow developer! If you've ever written in JavaScript, you've surely encountered a situation where you need to remove a specific element from an array. It seems like a simple task, but beginners (and even experienced programmers) often get confused: splice? filter? Or maybe delete? Or should I just reinvent the wheel?
\n\nThe question "How can I remove a specific item from an array in JavaScript?" is one of the most popular on Stack Overflow with millions of views. And this is no coincidence: arrays are the foundation of working with data in JS, and knowing how to clean them properly is a must-have skill.
\n\nIn this article, we'll break down 5 proven ways to remove an element from an array in JavaScript. You'll learn which method is faster, which is cleaner to read, and in which situations each one is best to use. Let's go!
\n\n1. The splice() method — a classic
\n\nLet's start with the most popular method — splice(). This method modifies the original array by removing, replacing, or adding elements. To remove one element, the syntax looks like this:
array.splice(index, 1);\n\nThe first parameter is the index of the element to remove, the second is the number of elements to remove (1 means we remove only one).
\n\nExample:
\n\nlet fruits = ['apple', 'banana', 'pear', 'orange'];\n\n// Remove 'banana' (index 1)\nfruits.splice(1, 1);\n\nconsole.log(fruits); // ['apple', 'pear', 'orange']\n\nImportant: splice() modifies the original array. If you need a copy — first do let copy = [...array].
When to use: when you know the exact index of the element. Ideal for removing by position.
\n\n2. The indexOf() + splice() combination — removal by value
\n\nBut what if we don't know the index, but we know the value itself? Here indexOf() helps us — it finds the first index of an element with the given value (or returns -1 if the element is not found).
let numbers = [10, 20, 30, 40, 50];\nlet itemToRemove = 30;\n\nlet index = numbers.indexOf(itemToRemove);\nif (index !== -1) {\n numbers.splice(index, 1);\n}\n\nconsole.log(numbers); // [10, 20, 40, 50]\n\nImportant: this method removes only the first occurrence. If there are multiple identical elements in the array, the rest will remain.
\n\nWhen to use: when you need to remove a specific array element by value, and you are sure the element appears only once.
\n\n3. The filter() method — for those who love cleanliness
\n\nThe filter() method is my personal favorite. It does not modify the original array but creates a new one containing only the elements that pass the test. This makes the code more predictable and safe.
let numbers = [10, 20, 30, 40, 50];\nlet itemToRemove = 30;\n\nlet filteredNumbers = numbers.filter(num => num !== itemToRemove);\n\nconsole.log(filteredNumbers); // [10, 20, 40, 50]\nconsole.log(numbers); // [10, 20, 30, 40, 50] (original unchanged)\n\nPlus: removes all occurrences of the value. If there were two numbers 30 in the array, both will disappear.
\n\nMinus: creates a new array, which can be less efficient when working with huge arrays (but for 99% of tasks this is not critical).
\n\nWhen to use: when immutability is needed (not changing the original data) or when you need to remove all duplicates.
\n\n4. The delete operator — dangerous but fast
\n\nThe delete operator removes an object property. Since an array is also an object, it can be applied to an array as well. But be careful: it leaves a "hole" (an empty element).
let fruits = ['apple', 'banana', 'pear'];