DEV Community

Athithya Sivasankarar
Athithya Sivasankarar

Posted on • Edited on

Types of Functions in JavaScript

1. Function Declaration

This is the basic way to create a function.

Example:

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

sayHello();
Enter fullscreen mode Exit fullscreen mode

Output:

Hello

2. Function Expression

A function expression is a function that is created and assigned to a variable (or used as a value).

Example:

const sayHello = function() {
    console.log("Hello");
};

sayHello();
Enter fullscreen mode Exit fullscreen mode

Output:

Hello

3. Arrow Function (Lambda)

Short and modern way to write functions.

Example:

const sayHello = () => {
    console.log("Hello");
};

sayHello();
Enter fullscreen mode Exit fullscreen mode

Output:

Hello

4. Anonymous Function

Function without a name.

Example:

const greet = function() {
  return "Hi there!";
};

console.log(greet());
Enter fullscreen mode Exit fullscreen mode

Output:

Hi there!

5. Callback Function

A function passed inside another function.

Example:

function greet(name, fun) {
    fun(name);
}

greet("Ram", function(name) {
    console.log("Hello " + name);
});
Enter fullscreen mode Exit fullscreen mode

Output:

Hello Ram

6. IIFE (Immediately Invoked Function)

Runs immediately after writing.

Example:

(function() {
    console.log("Run now");
})();
Enter fullscreen mode Exit fullscreen mode

Output:

Run now

7. Constructor Function

Used to create objects.

Example:

function Person(name) {
    this.name = name;
}

let p1 = new Person("Ram");
console.log(p1.name);
Enter fullscreen mode Exit fullscreen mode

Output:

Ram

8. Generator Function

Used to return values one by one.

Example:

function* num() {
    yield 1;
}

let n = num();
console.log(n.next().value);
Enter fullscreen mode Exit fullscreen mode

Output:

1

Reference

Top comments (4)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Function expression and storing a function in a variable are two different things. You haven't explained what function expression is.

Collapse
 
athithya_sivasankarar profile image
Athithya Sivasankarar

What is the function expression? We should put the function inside the variable that is called a function expression .

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Function expression occurs when the function keyword is used at a point in the code where a statement would be illegal - i.e. as part of an expression. In this situation it kind of functions as a 'function literal' - defining a function in-place, but not assigning it to any variable. A function created in this manner CAN be assigned to a variable - and often is... but the act of assigning it to a variable has nothing to with it being function expression.

Function expressions are often used to create anonymous callbacks to pass to other functions, and are also frequently seen in IIFEs - hence the name 'Immediately Invoked Function Expression'.

Thread Thread
 
athithya_sivasankarar profile image
Athithya Sivasankarar

thanks for the explanation sir