Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What is the first class function in JavaScript ?

  • Last Updated : 19 Sep, 2021

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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
Recommended Articles
Page :

Start Your Coding Journey Now!