Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is functional programming in JavaScript

Discovering the Functional Programming Paradigm in JavaScript

Functional programming (FP) is a programming paradigm, a style or "way" of programming, that treats computation as the evaluation of mathematical functions, avoiding changing state and mutable data. Let's make this simpler: when we say "functional programming," we're talking about a way to create programs that only focus on what the program should accomplish, not how it should be done.

The Core Principles of Functional Programming

Before we delve into functional programming in JavaScript, let's acquaint ourselves with a few core principles of functional programming:

Immutability: This is the idea that once a data value is created, it can never change. If you want to modify a value, you make a copy of it and modify the copy. In JavaScript, this could be as simple as assigning a variable to a new value.

Pure functions: A pure function is a function that, given the same inputs, will always return the same output, and has no side-effects. "Side-effects" is a term that means the function does something other than compute the result, like modifying a global variable, printing a value, etc.

First-class and higher-order functions: JavaScript treats functions as values - you can assign them to variables, use them as arguments in other functions, or return them from other functions. This is known as treating functions as "first class citizens." A higher-order function is a function that takes another function as an argument, returns a function, or both.

Recursion: Instead of using loops, functional programming often utilizes recursion. Recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem.

A Glimpse of Functional Programming in JavaScript

Let's look at some examples of how we can implement functional programming in JavaScript:

Immutability

let a = 1;
let b = a; // create a copy of a
b = 2; // modify the copy
console.log(a); // outputs 1

In this example, modifying b does not affect the value of a.

Pure Functions

function add(a, b) {
  return a + b;
}
console.log(add(1, 2)); // outputs 3

The add function is a pure function. It always gives the same output for the same inputs and does not modify anything outside the function.

First-Class and Higher-Order Functions

function greet(name) {
  return `Hello, ${name}!`;
}

function yell(fn, name) {
  return fn(name).toUpperCase();
}

console.log(yell(greet, 'John')); // outputs 'HELLO, JOHN!'

In this example, greet is a first-class function that we pass into yell, which is a higher-order function.

Recursion

function countDown(number) {
  console.log(number);
  if (number > 0) {
    countDown(number - 1);
  }
}
countDown(5); // Outputs 5 4 3 2 1

Here, countDown is a recursive function that calls itself until number is no longer greater than 0.

The Power of Functional Programming

Functional programming can make your code cleaner, more predictable, and easier to test. But, it can take some time to wrap your head around. It's like learning to cook a whole new cuisine. The ingredients and utensils may seem strange and unfamiliar at first, but as you get to know them, you'll start to cook up some amazing dishes.

Functionally Concluding

In the vast terrain of JavaScript, functional programming is like a tranquil garden, where each function is a unique, blooming flower. Stroll down the path of this garden, and you'll find your mind opened to new ways of crafting your code. Each function, pure and independent, contributes to the overall beauty of your program. As you embrace immutability, you'll discover a sense of calm as the chaos of changing states fades away. As you hold the hand of recursion, you might feel like you're walking in circles, but soon you'll realize that you're spiraling upward, reaching new heights of clarity and understanding. Embarking on this journey may seem daunting, but remember, every great journey starts with a single step, or in our case, a single function call. Happy Functioning!