Skip to content

Types of functions in JavaScript | Basics

  • by

JavaScript has three types of functions. A function should take some input and return an output where there is some logic between the input and the output.

Types of functions in JavaScript

Simple example code of all types of functions available in JS.

Named function: Simple define it in the code and then call it. The below function returns the product of p1 and p2.

<!DOCTYPE html>
<html>
<body>
  <script >
    function mult(p1, p2) {
      return p1 * p2;   
    }

    console.log(mult(20,50))
  </script>
</body>
</html>

Output:

Types of functions in JavaScript

Anonymous function: is a function that does not have any name associated with it. A JavaScript function can also be defined using an expression.

const x = function (a, b) {return a * b};
console.log(x(20,50))

Immediately invoked function expression: is a JavaScript function that runs as soon as it is defined.

function()  
 {  
 console.log("Hello World");   
})();  

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.