Callback function parameters

Hi,
I have been doing a course JavaScript syntax part II and at a code challenges: intermediate JavaScript: justCoolStuff()

I can’t understand how this code works:

function justCoolStuff(firstArray) {
  function isInSecondArray(item) {
   return item
  }
  return firstArray.map(isInSecondArray);
}
const coolStuff = [
  "gameboys",
  "skateboards"
];

console.log(justCoolStuff(coolStuff));  // print [ 'gameboys', 'skateboards' ]

Me question is why does a return item
print [ 'gameboys', 'skateboards' ] ?

I didn’t specifiy item I only specify isInSecondArray without parenthesis. If I return item or return isInSecondArray the results are the same.

The .map() method takes a callback and then runs that function on each element in the array it is acting upon. The return from that is another array. We hand the function by reference (by name only) without invoking it. The map method will invoke it as many times as there are elements in the array.

While the code is kind of pointless, in its present form, it is a demonstration of the mechanics of higher order functions (that take a function)).

1 Like