What if my callback functions consist of parameters

As the title suggests, can I use them as callback functions? If that’s the case, how can I invoke them?

You may have to create an extra function in between.

function addAndPrint(x, y) {
  console.log(x + y);
  return x + y;
}

for example, if you want to use add for the callback function, but you want to use a specific argument for the second function, you would make a new function to do that

let arr = [1, 4, 9, 15];
arr.forEach( function callback(x) { addAndPrint(x, 2); } );

notice that I couldn’t do arr.forEach(addAndPrint) because I wanted to set the second argument of addAndPrint (so that it’s always 2 in my case).

Thanks Jan! However, I’ve got another question to ask. What if my callback functions consist of default parameters? How do I call out them?

const WelcomingStatement = (WelcomeGuestName = ‘Guest’) => console.log(Welcome ${WelcomeGuestName}!)
const FarewellStatement = (FarewellGuestName = ‘Guest’) => console.log(Goodbye ${FarewellGuestName}!)
const WelcomeOrFarewell = (Time,WS,WGN,FS,FGN) => {
if(Time === ‘Day’){
WS(WGN);
}else if(Time === ‘Night’){
FS(FGN);
}else{
console.log(‘Invalid Time.’)
}
}

If you’d like to use the default parameter, you could leave it out when you call the function.
Here’s an example:

function addAndPrint(x, y = 1) {
  console.log(x + y);
  return x + y;
}

notice that the second parameter has default parameter 1.
If I’d want to use that default value, then I’d call the function - a version with no second argument.

let arr = [1, 4, 9, 15];
arr.forEach( function callback(x) { addAndPrint(x); } ); 

Notice the second argument of addAndPrint is not there, which is the same as putting 1 for the second argument.