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