user13964720
user13964720

Reputation:

How does this parameter knows the value of arr

How does the elem knows the value of arr

function a(num){
  function ab(elem){
        let num=6
        return elem.length>num;
    }
    return ab;
  }

let arr=['caterpillar','justin','openhome'];
console.log(arr.filter(a()));

Upvotes: 1

Views: 32

Answers (1)

Prakhar
Prakhar

Reputation: 1485

The return value of executing the function a is function ab. This is being passed as the callback to arr.filter. filter() calls the provided callback function once for each element in the array. Hence function ab will receive the passed value in elem.

Apart from the current element being processed, filter() also passes index of the current element and the original array in this form callback( element , index, originalArray )

To receive those values in function ab just add two more parameters like so

ab(elem , index, arr)

Array.prototype.filter()

Upvotes: 0

Related Questions