Functional programming in JavaScript

Functional programming has become a really hot topic in the JavaScript world. Just a few years ago, few JavaScript programmers even knew what functional programming is, but every large application codebase I’ve seen in the past 3 years makes heavy use of functional programming ideas.

Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Contrast with object oriented programming, where application state is usually shared and colocated with methods in objects.

Functional programming is a programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles (listed above). Other examples of programming paradigms include object oriented programming and procedural programming.

Functional Programming (FP) is a programming paradigm with some particular techniques.

In programming languages, you’ll find purely functional programming languages as well as programming languages that support functional programming techniques.

Haskell, Clojure and Scala are some of the most popular purely functional programming languages.

Popular programming languages that support functional programming techniques are JavaScript, Python, Ruby and many others.

Functional Programming is not a new concept, actually its roots go back o the 1930’s when lamda calculus was born, and has influenced many programming languages.

FP has been gaining a lot of momentum lately, so it’s the perfect time to learn about it.

In this course I’ll introduce the main concepts of Functional Programming, by using in the code examples JavaScript.

Why Functional Programming

  • Its pure function, provides confidence of not changing things outside of its scope.

  • Its reduces the complexity, need not to worry about how it is doing it, focus will be only on what it is doing.

  • Ease of testing, because it does not depend on state of the application and result verification also will be easy.

  • It makes the code more readable.

  • Functional programming makes code easier to understand.

Examples of Functional Programming

Array Functions

Functional Programming

In the above example, we are trying to filter only active meet-ups and we can see it can be achieved with two different approach. Here, second approach is Functional Programming where filter() method takes care of “how program operates” and program focuses only on input i.e. meetups array and care about output activeMeetupsFP but in first approach program cares about how to operate as well with for loop.

First class functions

In a functional programming language, functions are first class citizens.

They can be assigned to variables

const f = (m) => console.log(m)
f('Test')

Since a function is assignable to a variable, they can be added to objects:

const obj = {
  f(m) {
    console.log(m)
  }
}
obj.f('Test')

as well as to arrays:

const a = [
  m => console.log(m)
]
a[0]('Test')

They can be used as an argument to other functions

const f = (m) => () => console.log(m)
const f2 = (f3) => f3()
f2(f('Test'))

They can be returned by functions

const createF = () => {
  return (m) => console.log(m)
}
const f = createF()
f('Test')

Higher Order Functions

Functions that accept functions as arguments or return functions are called Higher Order Functions.

Examples in the JavaScript standard library include Array.map()Array.filter() and Array.reduce(), which we’ll see in a bit.