DEV Community

Rishad Karappa
Rishad Karappa

Posted on

First Order Function vs First Class Functions (JavaScript) Many beginners get confused between first order function and first class functions.

šŸ”¹ First Order Function

A First Order Function is a function that:

āŒ Does NOT take another function as argument

āŒ Does NOT return another function

It just works with normal values (number, string, etc).

`Example:
function multiply(a, b) {
  return a * b;
}`
Enter fullscreen mode Exit fullscreen mode

Here:

It takes numbers

Returns a number

No function involved

So this is a First Order Function.

šŸ‘‰ It is just a normal/basic function.

šŸ”¹ First Class Functions

JavaScript supports First Class Functions.

This means:

šŸ‘‰ Functions are treated like normal values (like numbers or strings).

Because of this, you can:

āœ… Store function in a variable

āœ… Pass function as argument

āœ… Return function from another function

1ļøāƒ£ Store Function in Variable
function greet() {
return "Hello";
}

const sayHi = greet;

console.log(sayHi()); // Hello

Function stored in variable → First class behavior.

2ļøāƒ£ Pass Function as Argument
function greet() {
console.log("Hello");
}

function execute(fn) {
fn();
}

execute(greet);

Here function is passed as parameter.

3ļøāƒ£ Return Function from Function
function outer() {
return function inner() {
console.log("Inside inner");
};
}

const result = outer();
result();

Function returning another function.

🧠 Important Understanding

āš ļø First Order Function and First Class Function are NOT opposites.

First Order → Type of function

First Class → Feature of language

Because JavaScript is a first-class function language,
we can create higher-order functions also.

šŸ”„ Small Comparison

First Order Function First Class Function
No function inside Functions treated like values
Simple function Language capability
Example: add(), multiply() Store, pass, return function

Final Simple Line

šŸ‘‰ First Order Function = Normal function
šŸ‘‰ First Class Function = JS allows functions to behave like variables

Top comments (0)