Functional Programming in JavaScript | Higher-order functions

What is functional programming?

In computer science, functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

In very simple terms, functional programming treats functions as values. It revolves around taking functions as arguments and returning functions as values.

Functional programming in JavaScript

One of the key points that makes JavaScript great for functional programming is the way it handles functions.

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

Another reason is JavaScript’s ability to use higher-order functionsa function that can take another function as an argument or return one as a result. You might be already aware of this in JavaScript (think about callback functions). When passing in functions as arguments, the function isn’t executed immediately (only the function definition is passed). It’s later called-back by the containing function.

Check out the following example on codepen:



See the Pen Higher-order functions by Divyanshu (@divyanshu013) on CodePen.dark