JS Functions are First-Class citizens! What does this mean?

You may hear of this in JavaScript as a Software Developer many times, but what does this mean actually?

Sina Farhadi
1 min readFeb 8, 2022

First-Class citizens in the world of programming

Basically, First-Class citizenship means that an entity that supports all the operations generally available to other entities or we can simply say being able to do what everyone else can do

First-Class citizens in JavaScript

In JavaScript, functions are objects (hence the designation of first-class object). They inherit from the Object prototype and they can be assigned key: value pairs.

function objects can be assigned to variables, they can be passed around as arguments; they can even be assigned as the return values of other functions. Demonstrably, functions in JavaScript are first-class objects.

Let’s see an example:

In the top example, we are passing a function sims like a variable to the console.log method & JS invoked the say_hello() function & passes the returned value from the function to the console.log

The behavior of the say_hellofunction was like a variableso we can say that functions are First-Class citizens or simply function can behave like a variable or other things that are not supposed to

--

--

Sina Farhadi
Sina Farhadi

Written by Sina Farhadi

Senior Frontend Developer | Expert in React.js, Next.js, and Svelte // Looking for new opportunities 🏄‍♂️ https://bugpointer.dev

No responses yet