user13964720
user13964720

Reputation:

How this parameter knows the argument

Im confused with how the elem get the value of arr?

function lengGreaterThan(num) {
  function lengGreaterThanNum(elem) {
      return elem.length > num;
  }
  return lengGreaterThanNum;
}

let arr = ['Justin', 'caterpillar', 'openhome'];
console.log(arr.filter(lengGreaterThan(6)));

Upvotes: 0

Views: 37

Answers (1)

MauriceNino
MauriceNino

Reputation: 6757

That is a really confusing way of writing it, but essentially you are just putting a function that takes one parameter into the filter function.

This here would do the same thing:

console.log(arr.filter((elem) => {
    return elem.length > 6;
}));

Upvotes: 0

Related Questions