Sunday, 5 June 2016

Passing A Function As A Parameter To Another Function In JavaScript

Consider the following example:-

pass_func.html:-


<html>
<head>
<body>
  <script src="app.js">
  </script>
</body>
</html>


app.js:-

function log(a)   //passing a function as a parameter to another function
{
  a();
}

log(function(){
   console.log("hi");
});


Output:-



Explanation:-

Here, in this example, "log()" function is declared with a parameter called "a", within which "a()" is declared. At the time of defining the "log()", another function is passed, in which the body of the passed function is coded or scripted.

No comments:

Post a Comment