Home Courses Types of Functions in JavaScript

Types of Functions in JavaScript

All types of functions in JavaScript. That means you will learn about different types of functions, their definitions, and simple examples. This topic is important for interviews because questions are often asked about function types.


First, we will start with Function Declaration.

This is the simplest and most common way to create a function in JavaScript. You use the function keyword, give it a name, add parameters if needed, and write code inside curly braces. Then you call the function using its name.


Next is Function Expression.

In this type, a function is assigned to a variable or constant. After that, the function is called using that variable name. It does not require calling the function using its original name.


Then comes Arrow Function.

This is a shorter and cleaner way to write functions. It uses arrow syntax (=>). It is commonly used in modern JavaScript and frameworks like React. It can also take parameters and return values like other functions.


After that, we have Functions with Parameters.

You can pass values inside functions so that the function works dynamically. Instead of fixed values, you can pass different inputs and get different outputs.


Next is Anonymous Function.

These are functions without a name. They are usually used inside variables or passed as arguments.

Then we have IIFE (Immediately Invoked Function Expression).

These functions are executed immediately after they are created. They are wrapped inside parentheses and called right away.


Now, let's understand Return vs Console.

  1. return sends a value back from a function.
  2. console.log only prints the value in the console.
  3. This is a common confusion for beginners.

Next is Higher Order Function (HOF).

A function that takes another function as a parameter or returns a function is called a higher-order function.

Then comes Callback Function.

A function that is passed as a parameter to another function is called a callback function.

After that, we have Generator Functions.

These are special functions that can pause and resume execution. They use the yield keyword and return values step by step.


Finally, we have Async Functions.

These are used when working with APIs or operations that take time. They return promises and are handled using .then() or other methods.

Share this lesson: