const numbers = [65, 44, 12, 4];
const tPass = 'hello';
numbers.forEach(myFunction)
function myFunction(item, index) {
console.log(item);
}
how do i pass tPass value to myFunction, and display for example console.log(tPass + item);
const numbers = [65, 44, 12, 4];
const tPass = 'hello';
numbers.forEach(myFunction)
function myFunction(item, index) {
console.log(item);
}
how do i pass tPass value to myFunction, and display for example console.log(tPass + item);
The forEach method skeleton can have the initial (item, index) parameters as you wrote before. And so in order to modify that, you have to make sure that you are always returning a function with the same argument structure.
Meaning: You declare a higher order function which we'll call myHigherFunction. myHigherFunction takes "tpas" and applys the logic for myFunction inside it, and returns it at the end. Finally, you pass tPass to myHigherFunction.
So the code becomes as follows:
const numbers = [65, 44, 12, 4];
const tPass = 'hello';
numbers.forEach(myHigherFunction(tPass))
function myHigherFunction(tPass){
function myFunction(item, index) {
console.log(tPass + item);
}
return myFunction
}
Remember to always return a function with the required skeleton (initial 2 parameters).