Seek and destroy - function call within indexOf()?

I just don’t understand how “function (arg)” works. It has no value, so how does the code know what its looking for within the indexOf()?

“arr” is arguments[0], and “newArray” is the array I made for arguments[1] and [2] etc…but, what is “arg”?

return arr.filter(function (arg) {
return newArray.indexOf(arg) === -1;

I don’t recall the challenge (it’s been some time), but if your question is about the .filter function and its callback, most array functions like this will take a callback function who will receive an element from the array as parameter.

So if arr = [1,2,3,4], then arr.filter() will loop over this array and function(arg){} will become function(1){}, then function(2){}, then function (3){} and finally function(4){}.

For filter you can then use this argument to make a boolean expression.


const allEvens = arr.filter(function(arg) {
  return arg % 2 === 0;

…which will return true for all even numbers and false for odd ones. allEvens will become an array of all the even numbers our of arr.

In your own example above, newArray.indexOf(arg) === -1 will return true if arg (any element from your original array) was found in newArray. So your filter will filter out all those elements that exist in both the old and the new array.

Is that what you were asking?

Great response, thank you.

The main point - what I was looking for - I now understand to be that “function (arg)” is iterations of the array with which we are using filter() - in this case, “arr”.

I do understand the rest of the logic, but was stuck on what “arg” consisted of. Thank for you the other example as well.