What are First Class Functions in JavaScript?
Learn via video course
When functions in a programming language are treated like any other variable then that programming language is known to have first-class functions. In javascript, the functions are known as the first-class citizens, which means functions can do what any other variables can. First-class functions javascript get this ability by treating the functions as an object.
As functions are treated like a variable, we can pass them as a parameter to the other function and return the function from another function just like any other variable. Because functions are treated as variables we can store them in any other variable, objects, and in an array. This simply means first-class functions in javascript are simply like values or like any other objects in the code.
Examples for First Class Functions Javascript
Following are the examples where we treat the functions as the first class functions in javascript.
Example 1: Assign function to a variable In the following coding example you can see how to assign the function to a variable.
Code Example:
Output:
As you can see in the above coding example, we declared the constant variable with the name myVariable and assigned the function to this particular variable. Now we can use myVariable like any other function to make a function call.
Example 2: Pass function as an Argument The following coding example shows how to pass the function as an argument.
Code Example:
Output:
In the above coding example, we have the function named wishHappyNewYear which returns the string 'Happy New Year, '. Here the function named wishPerson takes two parameters, the first argument which we passed at the function call is the wishHappyNewYear function and the second argument is the name of the person (i.e. 'John Doe').
Example 3: Return a function and call using another variable.
In the following coding example we returned the function from another function. The returned function is assigned to another variable and called a normal function.
Code Example:
Output:
In the above coding example, we returned the function from the sayHello function. We assigned this returned function in the newFun variable which is similar to the assignment we did in the 1st example. Then we called the newFun variable like a normal function.
Example 4: Return a function and call using double parentheses
In the following coding example we returned the function similar to what we did in the above example but to make a function call we used double parentheses.
Code Example:
Output:
In the above coding example, we returned the function from the sayHello function similar to the 3rd coding example. Here we used ()() in two parentheses to make the function call. This concept in javascript is called the function currying where the first parentheses invoke the sayHello function and the second parentheses invoke the returned function and we get the above output.
Learn more
In this article, we saw the first class functions javascript, to learn more about javascript functions you can refer to the following article.
Conclusion
- In this article, we saw what is first-class function javascript.
- JavaScript treats its function as first-class citizens by giving the ability to treat functions like the variable.
- We saw various ways to leverage the first-class functions with the coding examples.
- We can pass the functions as an argument, assign the functions to another variable, and return the functions as an object or variable in the javascript.