Functions that Accept other Functions as Parameters and Return the Functions:
Consider the following program that updates an array and prints it out along with an input passed to the inner function.
xxxxxxxxxx
14
const names= ['John', 'Tina','Kale','Max']
function useReturnFunc(arr,fn) {
for(let i = 0; i < arr.length; i++) {
arr[i]= fn(arr[i]) ;
}
return function secondFunction(y) {
console.log("First argument is an array: "+ arr);
console.log("Argument passed to secondFunction is "+ y );
};
}
function argFn (name){
return("Hello " + name );
}
useReturnFunc(names,argFn)("a string.");
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment