Skip to content
Related Articles
Open in App
Not now

Related Articles

What is the first class function in JavaScript ?

Improve Article
Save Article
  • Last Updated : 19 Sep, 2021
Improve Article
Save Article

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 treat function as a first-class-citizens. 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




<script>
    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!!!`;
        }
  
    }
  
    document.write(Arithmetics.add(100, 100) + "<br>");
    document.write(Arithmetics.subtract(100, 7) + "<br>");
    document.write(Arithmetics.multiply(5, 5) + "<br>");
    document.write(Arithmetics.division(100, 5));
</script>

Note: In the above example, functions are stored as a variable in an object.

 

Output:

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

Example 2:

Javascript




<script>
    const Geek = (a, b) => {
        return (a + " " + b);
    }
      
    document.write(Geek("Akshit", "Saxena"));
</script>

Output:

Akshit Saxena

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!