Difference Between Two Arrays in JavaScript? Last Updated : 12 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the following ways to Get the Difference Between Two Arrays in JavaScript:1. Using the filter() and includes() Methods - Mostly UsedWe can use the filter() method on the first array and check if each item is not present in the second array using the includes() method. The resulting array contains the elements that are present in the first array but not in the second array. JavaScript let a1 = [1, 2, 3, 4, 5]; let a2 = [3, 4, 5, 6, 7]; let res = a1.filter((e) => !a2.includes(e)); console.log(res); Output[ 1, 2 ] 2. Using for Loop and indexOf() MethodTo find the difference between two arrays in JavaScript, you can iterate over one array using a for loop and check if each element exists in the other array using the indexOf() method. If an element is not found, it is added to the result array. JavaScript let a1 = [1, 2, 3, 4, 5, 0]; let a2 = [3, 4, 5, 6, 7, 9]; let res = []; for (let i = 0; i < a1.length; i++) { if (a2.indexOf(a1[i]) === -1) { res.push(a1[i]); } } console.log(res); Output[ 1, 2, 0 ] 3. Using Set and filter() MethodThe arrays are converted to Sets using the Set constructor. The Set data structure only allows unique values. By filtering out the elements from the first set that also exist in the second Set, we can obtain the difference. JavaScript let a1 = [1, 2, 3, 4, 5]; let a2 = [3, 4, 5, 6, 7]; let s1 = new Set(a1); let s2 = new Set(a2); let res = [...s1].filter( (e) => !s2.has(e)); console.log(res); Output[ 1, 2 ] 4. Using reduce() and includes() MethodsThis method iterates over the first array and checks if each element is present in the second array using the includes() method. It accumulates the elements that are not found in the second array, resulting in the difference between the two arrays. JavaScript let a1 = [1, 2, 3, 4, 5]; let a2 = [3, 4, 5, 6, 7]; let res = a1.reduce((r, e) => { if (a2.indexOf(e) === -1) { r.push(e); } return r; }, []); console.log(res); Output[ 1, 2 ] 5. Using Lodash _.difference() FunctionLodash _.difference() function is used to remove a single element or the array of elements from the original array. This function works pretty much the same as the core function of JavaScript i.e. filter. JavaScript let _ = require("lodash"); let a1 = [1, 2, 3]; let a2 = [2, 3] let res = _.difference(a1, a2); console.log(res); Output:[1]6. Using Array.filter() and Array.every() MethodsWe are using the filter() method along with the every() method. We iterate over each element of the first array and check if it exists in the second array using the every() method. If an element is not found in the second array, it is included in the resulting array. JavaScript let a1 = [1, 2, 3, 4, 5]; let a2 = [3, 4, 5, 6, 7]; let res = a1.filter((e) => a2.every((val) => val !== e)); console.log(res); Output[ 1, 2 ] 7. Using Array find() and indexOf() MethodsThis approach utilizes the find() method along with the indexOf() method to efficiently find elements in one array that are not present in the other array. JavaScript let a1 = [1, 2, 3, 4, 5]; let a2 = [3, 4, 5, 6, 7]; let res = a1.filter((e) => a2.indexOf(e) === -1); console.log(res); Output[ 1, 2 ] Comment More infoAdvertise with us Next Article Difference Between Two Arrays in JavaScript? P pranay0911 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads Difference Between JavaScript Arrays and Objects Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p 1 min read Difference Between Array.from and Array.of in JavaScript JavaScript provides various methods for creating and manipulating arrays, two of which are Array.from and Array.of. These methods are part of the ECMAScript 6 (ES6) specification and offer distinct ways to create arrays. Understanding the differences between these two methods is important for effici 3 min read Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat 3 min read How to get symmetric difference between two arrays in JavaScript ? In this article, we will see how to get the symmetric difference between two arrays using JavaScript. In Mathematics the symmetric difference between two sets A and B is represented as A ΠB = (A - B) ⪠(B - A) It is defined as a set of all elements that are present in either set A or set B but not 6 min read Print Difference between Two Objects in JavaScript ? Printing the difference between two objects in JavaScript involves comparing the properties of the two objects and identifying the discrepancies between them. This can include properties that exist in one object but not in the other, as well as properties with differing values. These are the followi 3 min read Difference Between array.reduce() and reduceRight() in JavaScript The array.reduce() and array.reduceRight() are two array methods used for reducing an array to a single value. They are particularly useful for performing cumulative operations on the array elements.Although they share similar functionality, their behaviour differs in the direction they iterate thro 3 min read Difference between forEach and for loop in Javascript In JavaScript, both forEach and for loops are used to iterate over arrays or collections, but they differ in usage and behavior. forEach is a higher-order function that simplifies iteration with built-in array methods, while for loops offer more control, flexibility, and broader application.For Loop 4 min read Difference between forEach() and map() loop in JavaScript The forEach() and map() methods in JavaScript are used to iterate over arrays, but they serve different purposes. forEach() executes a provided function once for each array element without returning a new array, while map() transforms elements and returns a new array.JavaScript forEach() JavaScript' 4 min read Get the Difference between Two Sets using JavaScript To get the difference between two sets in JavaScript, you need to identify elements present in the first set but not in the second. This involves iterating over the first set and filtering out elements that are also found in the second set, ensuring efficient comparison. We can get the difference be 2 min read What is the difference between Array.slice() and Array.splice() in JavaScript ? In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements. slice():The slice() method in JavaScript extracts a sec 3 min read Like