0

I am doing intermediate algorithms on FREECODECAMP and on the exercise Diff Two Arrays,

The exercise is:


Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.


I used for loops to solve the problem but I found another solution that intrigues me. It is below:

function diffArray(arr1, arr2) {

    return arr1

    .concat(arr2)
    .filter(
        item => !arr1.includes(item) || !arr2.includes(item)
    )
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

I cannot seem to find out the syntax for the "=>" that they are using? Can someone help me understand what the "=>" is doing? Thanks a ton for your help.

Shezmula
  • 47
  • 8
  • Anonymous function (lambda). It lets you pass a function as an argument. Google search [JS arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). – Ashwin Gupta Dec 01 '16 at 02:09
  • This answers everything. Sorry to have asked a questions that already existed. I couldn't find it. I will try harder next time. Thank you again! – Shezmula Dec 01 '16 at 02:31
  • NP. For the future, you can always search in the SO search Q:A bar on the top right also for the Q:A style help! :D, good luck with your arrow functions. – Ashwin Gupta Dec 01 '16 at 02:32

0 Answers0