Callback function inquiry

Considering the following code snippet:

function myFunction(param){
param('Learn to code;);
}
function myCallback(value){
console.log('Value:', value);
}
myFunction(myCallback);

I am confused as to param('Learn to code'). I don’t understand how it runs and couldn’t find documents that might explain this.

I would really appreciate it if you can offer an explanation! Thank you!

Hi @text9497901072 ,

To help you better in visualizing, you can refer to the following quotes along with its respective codeblocks of explanation. From here, I’m assuming that you understand the use of Callback functions, otherwise you can refer to this link for reference.

In this example, what the variable ‘param’ is behaving as a function call, and its duty, is to perform your callback function’s mechanism, which in this case, is

console.log('Value:', value);

As you execute the function of “MyFunction”, your “myCallback” function became the parameter alone, and hence becoming the following with reference to your example,

// now, param = myCallback

// placeholder: function myFunction(param)

myFunction( myCallback(value) ) {
 // do something... 
};

and hence, your param parameter is now myCallback(value) function, which the value is now, ‘Learn to code’, and then been passed to the mechanism which is output to the console.

myFunction( myCallback(value) ) {
 myCallback('Learn to code');
};

Hope the above helps.

2 Likes