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?
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_hello
function was like a variable
so 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