JavaScript Callback Function Example
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Pragati
In this tutorial we will show you the solution of JavaScript callback function example, here we are going to show you example in callback function.
A JavaScript callback is a function which is to be executed after another function has finished execution.
A more formal definition would be – Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.
Step By Step Guide On JavaScript Callback Function Example :-
Here we defined myFun() function definition with two parameters (n1,n2) within that we declared another function fun() call there we passing n1,n2 parameters as argument and then we printing them on console after execution of fun().
It returns to myFun() function as like callback function then we returning parameters multiplication result to myFun() declaration result of multiplication stored on variable ‘res’ then we printed on console.
<!DOCTYPE html> <html> <head> <title>CALLBACK FUNCTION</title> </head> <body> <script> function myFun(n1,n2){ fun(n1,n2); return n1*n2; } function fun(v1,v2){ console.log('This is callback function and value 1 :'+v1+'; value 2 :'+v2+';'); } var res=myFun(20,50); console.log('Result of 20*50 is ',res); </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In script we defined function myFun() with two parameters ‘n1,n2’ which is declared as function call at last with values ’20,50’ as parameters.
- Within myFun() function we declared another function ‘fun()’ call with those myFun() parameters ‘n1,n2’ for pass them to another function there we just printing those ‘n1,n2’ values on console by referring them as ‘v1,v2’ parameters.
- Now we have comes to again ‘myFun()’ method there we multiplying ‘n1 and n2’ then returns to function call myFun() there we stores value to variable ‘res’ then finally we printed multiplication result on console.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to define callback functions in javascript.
When we executes program on browser we can’t see the result because we printed on console panel so we have to use shortcut ‘ctrl+shift+j’ then console panel will open at right hand side with results ‘This is callback function and value 1 :20; value 2 :50; , Result of 20*50 is 1000’.
If your wish to change those specified values at function declaration then you can modify it accordingly.
I hope this tutorial on JavaScript callback function example helps you and the steps and method mentioned above are easy to follow and implement.