DEV Community

Rishad Karappa
Rishad Karappa

Posted on

First Order Function vs First Class Functions (JavaScript)

🔹 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";
}
`
Enter fullscreen mode Exit fullscreen mode

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();
}`

Enter fullscreen mode Exit fullscreen mode

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();`
Enter fullscreen mode Exit fullscreen mode

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 Line

👉 First Order Function = Normal function
👉 First Class Function = JS allows functions to behave like variables

Top comments (0)