Skip to content

JavaScript pass function as parameter with arguments

  • by

No specific syntax is required to pass a function as a parameter with arguments in JavaScript. Just pass it like a regular argument with some logic.

JavaScript pass function as parameter with arguments

Simple example code defines the function argument like a regular argument and calls that argument like a function.

<!DOCTYPE html>
<html>
<body>
  <script>

    function fubar(a, b, fn){
      console.log("argument function")
      return fn(a, b);
    }

    function add(c, d){
      console.log("add function")
      return c + d;
    }

    // pass the "add" function like a regular argument
    let result = fubar(100, 200, add);

    console.log(result);
  </script>

</body>
</html> 

Output:

JavaScript pass function as parameter with arguments

Do comment if you have any doubts or suggestions on this JS function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *