Where does num come from in Higher Order Arrow Functions?

I´m having an issue with this topic.

Where does the num come from? How does the function knows that num refer to the elements of the array? As I see, there was no where when num was declared.

What do I think?
num can be replaced with any random word, because the way filter() woks is assigning array elements to be worked on with the function. But its just a speculation.

    const squareList = (arr) => {
      "use strict";
      const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
      return squaredIntegers;
    };

    // test your code
    const squaredIntegers = squareList(realNumberArray);
    console.log(squaredIntegers);

Thanks in advance!

1 Like

num is a parameter of the callback function. It is the placeholder for the first argument passed to it.

Thank you very much for your answer Randell.

Totally clear that num is a parameter, but how do the function know what does num refer to? Is it due to a syntax convention?

the method feeds the elements to the callback function one by one, and then do something with the returned values of the callback to create the returned value of the method

the callback is just a function, but the arguments that are passed in are the elements of the array because that is what the map and filter methods do

1 Like

The filter and map methods have been designed so that the first argument passed to the function represents the actual elements of the array, the second argument represents the indices of the array, and the third argument represents the original array in which the method was invoked. That is why if I have an array of numbers, I would typically name the first parameter num. If I had an array of movie titles, I would name the first parameter either movie or title or movieTitle. I typically name the second parameter index or idx.

2 Likes