Compare two arrays & save the mismatched data in another array

I need to compare two arrays & get the mismatched data from both into third array.

Details: following is the code I used for reference

var array1 = [1, 2, 3, 4, 8, 5, 6, 7];
var array2 = [6, 5, 4, 3, 2, 1] ;
var array3 = ;

for(var j=0; j<array1.length; j++)
{
var ar1 = array1[j];
//console.log(““+ar1+””)

for(var k=0; k<array2.length; k++)
{
    var ar2 = array2[k];
//console.log("####"+ar2+"####")

    if(ar1===ar2)
    {
        console.log(ar1+" is equal to "+ar2)
        break;
    }
    // else
    // if(k=array2.length)
    // {
    //     console.log("the value "+ar1+" is not found")
    // }
   
}

}

Please let me know if we can get mismatched data using this, else if you have any another way to compare & get mismatched data please provide the solution, Thanks !!!

The problem is that you are comparing one array to another. Therefore, you need to know which array has the most entries for the comparison. There are a lot of assumptions going on here, but something like the following…

var array1 = [1, 2, 3, 4, 8, 5, 6, 7];
console.log(array1);
var array2 = [6, 5, 4, 3, 2, 1];
console.log(array2);

if (array2.length >= array1.length) {
    var difference = array2.filter(n=>!array1.includes(n))
} else {
    var difference = array1.filter(n=>!array2.includes(n))
}

console.log(difference);

image