Skip to main content

Command Palette

Search for a command to run...

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

Most Asked JavaScript Interview Question

Published
1 min read
Difference between First-Class and Higher-Order Functions in JavaScript
P

I am a full stack MERN developer from Madhya Pradesh, India. Currently, I have experience of MERN Stack by my academic and own projects.

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.

For Example :

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));

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

Example :

const greet = function(name){
    return function(m){

        console.log(`Hi!! ${name}, ${m}`);
    }
}

const greet_message = greet('ABC');
greet_message("Welcome To jscodders")

Interview Series (MERN Stack)

Part 5 of 5

After a lot of research and discussion with developers, I am publishing this series of blogs.Please don't get demotivated by looking at these questions.

Start from the beginning

React JS Interview Questions

Maybe it will help you for your career