Author -  Sai gowtham

Difference between two arrays in JavaScript

In this tutorial, we are going to learn about how to get a difference between two arrays in JavaScript with the help of examples.

Consider we have two arrays like this.

const arr1 = [1,2,3,4];
const arr2 = [1,2,4,5];

The difference between the above two arrays is [3,5].

Let’s write the solution in JavaScript with the help of es6 filter() and includes() method.

function getDifference(arr1,arr2){
             return  arr1
                   // filtering difference in first array with second array
                 .filter(x => !arr2.includes(x))
                    // filtering difference in second array with first array
                 .concat(arr2.filter(x => !arr1.includes(x)));
}

const arr1 = [1,2,3,4];
const arr2 = [1,2,4,5];

getDifference(arr1,arr2); // [3,5]

Explanation

In the above code, we have first filtered the difference in the first array with the second array using filter and includes method then we chained with the concat method.

Inside the concat() method we have filtered the difference in the second array with the first array.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY