Another way to solve the problem
function diffArray(arr1, arr2) { return arr1.concat(arr2).filter(function (val) { if (!(arr1.includes(val) && arr2.includes(val))) return val; }); } diffArray([1, 2, 3, 7], [3, 2, 1, 4, 5]); // return [7, 4, 5] Also, you can use ES6+arrow function syntax:
const diffArray = (arr1, arr2) => arr1.concat(arr2) .filter(val => !(arr1.includes(val) && arr2.includes(val))); diffArray([1, 2, 3, 7], [3, 2, 1, 4, 5]); // return [7, 4, 5]