First-Class Function

A programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function.
JavaScript treats function as a first-class citizen. This means that functions are simply a value and are just another type of object.

Example: Let us take an example to understand more about the first-class function.

Javascript
const Arithmetics = {
    add:(a, b) => {
        return `${a} + ${b} = ${a+b}`;
    },
    subtract:(a, b) => {
        return `${a} - ${b} = ${a-b}`
    },
    multiply:(a, b) => {
        return `${a} * ${b} = ${a*b}`
    },
    division:(a, b) => {
        if(b!=0) return `${a} / ${b} = ${a/b}`;
        return `Cannot Divide by Zero!!!`;
    }

}

console.log(Arithmetics.add(100, 100));
console.log(Arithmetics.subtract(100, 7));
console.log(Arithmetics.multiply(5, 5));
console.log(Arithmetics.division(100, 5));

Output: In the above program, functions are stored as a variable in an object.

"100 + 100 = 200"
"100 - 7 = 93"
"5 * 5 = 25"
"100 / 5 = 20"

Difference between First-Class and Higher-Order Functions in JavaScript

Understanding the difference between first-class and higher-order functions in JavaScript is really important. These are big concepts in programming, especially in the kind of coding used for making websites. This article is all about explaining what they are, how they are used, and why they are so important.

Similar Reads

1. First-Class Function

A programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function.JavaScript treats function as a first-class citizen. This means that functions are simply a value and are just another type of object....

2. Higher-Order Function

A function that receives another function as an argument or that returns a new function or both is called Higher-order function. Higher-order functions are only possible because of the First-class function....

Key Differences between First-Order Function and Higher-Order Function

First-Order Function Higher-Order Function Function is treated as a variable that can be assigned to any other variable or passed as an argument.The function receives another function as an argument or returns First-order a new function or both.The “first-class” concept only has to do with functions in programming languages.The “higher-order” concept can be applied to functions in general, like functions in the mathematical sense.The presence of the First-class function implies the presence of a higher-order function.The presence of a Higher-order function does not imply the presence of a First-order function....

Contact Us