A first-class citizen (also type, object, entity, or value) in a given programming language is an entity which supports all the operations generally available to other entitiesSource
First class function are treated like variables. Variables are assigned values. They are passed as an arguments. Variables are returned. So let’s how these attributes are shared with function.
As an Assignment
Now we have the general idea. Let’s explain it in purely programmatical manner.
Now what we did is we have created a function and named it
sum. After that we are creating a variable
Side Note: Before the assignment the function was anonymous. The function is named function after assignment. Anonymous are sometimes hard to debug
First Class Function as Callbacks
We have some nice Array manipulation methods. Namely map, filter, reduce and more. The Map method accepts a function which is executed for each item of the array. Let’s see it in action
The square of prime numbers are stored in
squared variable. This
map method accepts function as an argument. Which is the property of First class function.
We call this behavior something like callback function. A function that runs inside another function.