Skip to main content

Command Palette

Search for a command to run...

First Class Function VS Higher Order Function (){ };

Updated
2 min readView as Markdown
First Class Function VS Higher Order Function (){ };
D
Software engineer building production systems with Node.js, TypeScript, Go, and PostgreSQL. Writing about backend engineering, infrastructure, and lessons from deploying real projects.

There has been a misconception about these two functions- First class and Higher order- being the same. In this article, I will make you have a better understanding between the two and their differences.

First Class Function:

This function is what we mostly create, All first class functions are written by the programmer(you). This function is treated as a first class citizen by Javascript, which means it's a simple value.

Application of this function:

(A)being passed as an argument to another function

const first = console.log('JavaScript');
      second.addEventListener('click', first);
//first is the first class function being passed

(B)being stored into variables or object property

const first = console.log('JavaScript');

const third = {

          functionProperty : function(){

..........
                                      }

the first example, stores a function into a variable. The second function is being stored in an object(third) property (functionProperty).

Higher Order Function:

This function is somewhat like methods already built on JavaScript and it's also a function that can be created as we code(just as the first class function), but the irony is that it needs a first class function to be passed into it as an argument to the function.

let's look at some examples to see what I mean, shall we :)

A built-in function that receives another function as an argument

      const first = console.log('JavaScript');
      second.addEventListener('click', first);
//addEventListener => this is the higher order function that receives a function as an argument

A customized(low key first class function) function that returns new function

function gain(){
let increase = 0;
return function(){ 
   increase+= 1;
}
}

//gain function(higher order function) is the customized or own-made function, that returns a new a function(first class function);

if this was helpful, do drop a feedback. LNL

#HAPPY CODING!