In this post, we compare two arrays, We have many simple ways to do this task in Javascript.
#1: Get the difference between two arrays in JavaScript?
To get the difference between two arrays we can use a Filter() function of javascript.
The filter()
the method creates a new array with all elements that pass the test implemented by the provided function.
let arrA = ['a', 'b'];
let arrB = ['a', 'b', 'c', 'd'];
let difference = arrA.filter(x => !arrB.includes(x)).concat(arrB.filter(x => !arrA.includes(x)));
console.log(difference);
Result
['c', 'd']
#2: Get the common or intersection elements of two arrays in JavaScript?
To get the difference or intersection between two arrays we can use a Filter() function of javascript.
let arrA = ['a', 'b'];
let arrB = ['a', 'b', 'c', 'd'];
let intersection = arrA.filter(x => arrB.includes(x));
console.log(intersection);
Result
['a', 'b']

Brijpal Sharma is a web developer with a passion for writing tech tutorials. Learn JavaScript and other web development technology.