JavaScript is a language that has first class functions, which in technical terms means that functions are so called first class citizens. In practice, that means that functions are treated like any other variable.

The following bullets represent important first-class function behavior:

  • Functions can be assigned to variables

  • Functions can be passed as arguments to other functions

  • Functions can be returned from other functions

 

Just to be clear First Class Functions is a feature that a programming language have or does not have. There are no first class functions in practise its just a concept. But, they are a necessity for “Functional Programming” paradigm.

The fact that JavaScript has First Class Functions makes it possible for us to use and write Higher Order Functions.

What about Higher Order Functions?!

Well, Higher-order functions are functions that take other functions as arguments or return functions as their results (output).  JavaScript comes with some built-in higher-order functions. You may already be using them, without realising that they are higher-order functions. Map, filter, and reduce are prime examples  and make common patterns like transforming, searching, and summing much easier!

Ok, lets go trough one example.

The above example uses an anonymous function as its callback. However, you can also do the same thing by creating and using a first class function.

Again, .map() is a high-order function. It allows a function to be passed as an argument. Higher-order functions allows you to write simpler and more elegant code. The map function is one of the many higher-order functions built into the language. sortreducefilterforEach are other examples of higher-order functions built into the language.

Thats all folks, for now.

 

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *