JavaScript- Delete all Occurrences in a JS Array Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report These are the following ways to remove all Occurrences of an element in a JS Array: 1. Using filter() method( Simple and Easy for Long Arrays)The filter() method creates a new array with all elements that passed the test implemented by the given callback function. JavaScript function fun(a, e) { if (!a.includes(e)) { return -1; } return a.filter( (item) => item !== e); } const a = [1, 2, 3, 4, 2, 5, 2]; const res = fun(a, 2); console.log(res); Output[ 1, 3, 4, 5 ] 2. Using splice() method(Efficient for Mid range Array)The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. JavaScript function fun(a, e) { if (!a.includes(e)) { return -1; } for (let i = a.length - 1; i >= 0; i--) { if (a[i] === e) { a.splice(i, 1); } } return a; } const a = [1, 2, 3, 4, 2, 5, 2]; const res = fun(a, 2); console.log(res); Output[ 1, 3, 4, 5 ] 3. Using forEach() method (Efficient for Short Array)The forEach() method will be used to iterate through each element of the array and make changes in it according to the condition passed inside the callback function. JavaScript function fun(a, e) { if (!a.includes(e)) { return -1; } let a1 = []; a.forEach(item => { if (item !== e) { a1.push(item); } }); return a1; } const a = [1, 2, 3, 4, 2, 5, 2]; const res = fun(a, 2); console.log(res); Output[ 1, 3, 4, 5 ] 4. Using reduce method(Efficient For Long Arrays)In this approach we use the reduce method to iterate over each element of the array and accumulate the elements that do not match the element to remove. JavaScript function fun(a, e) { return a.reduce((acc, c) => { if (c !== e) { acc.push(c); } return acc; }, []); } let a = [1, 2, 3, 4, 2, 5, 2]; let e = 2; let a1 = fun(a, e); console.log(a1); Output[ 1, 3, 4, 5 ] Comment More info A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like