A beginners guide to Functional Programing in Javascript(Part 1)

A beginners guide to Functional Programing in Javascript(Part 1)

·

5 min read

“While functions being unable to change state is good because it helps us reason about our programs, there's one problem with that. If a function can't change anything in the world, how is it supposed to tell us what it calculated? In order to tell us what it calculated, it has to change the state of an output device (usually the state of the screen), which then emits photons that travel to our brain and change the state of our mind, man.” ― Miran Lipovača

What is functional Programing

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. In other words, functional programming is a style of programming that emphasizes the use of functions to operate on data, rather than modifying data in place.

JavaScript is a popular programming language that is often used for building web applications and, increasingly, for building server-side and even desktop applications. JavaScript has many of the features that are commonly associated with functional programming languages, such as first-class functions and support for higher-order functions. This means that it is possible to write JavaScript code in a functional style, even if it is not a purely functional language.

Key benefits of functional programming

One of the key benefits of functional programming is that it makes it easier to write code that is easy to reason about and that is easy to test. This is because functional programming emphasizes the use of functions that are pure, meaning that they always return the same output for a given input, and that they do not have any side effects. This makes it easier to predict the behavior of a program, and to test it by simply providing input and verifying the output.

Another benefit of functional programming is that it makes it easier to write code that is concurrent and parallelizable. This is because functional programming emphasizes the use of immutable data, which means that data cannot be changed once it is created. This makes it easier to write code that can be run in parallel, because there is no need to worry about race conditions or other forms of synchronization.

To write functional JavaScript code, it is important to understand the concept of first-class functions, which are functions that can be treated like any other value. This means that they can be assigned to variables, passed as arguments to other functions, and returned from functions. This is an important concept in functional programming, because it allows functions to be composed and combined in powerful ways.

To illustrate how first-class functions can be used in JavaScript, consider the following example:

// Define a function that takes a function as an argument
function apply(func, arg) {
  // Return the result of calling the function with the given argument
  return func(arg);
}

// Define a function that adds two numbers
function add(x, y) {
  return x + y;
}

// Define a function that squares a number
function square(x) {
  return x * x;
}

// Apply the add function to the arguments 3 and 4
console.log(apply(add, 3, 4));  // Output: 7

// Apply the square function to the argument 5
console.log(apply(square, 5));  // Output: 25

In this example, the apply function takes a function and an argument, and then applies the function to the argument. This is an example of a higher-order function, because it takes a function as an argument. The add and square functions are first-class functions, because they can be passed as arguments to other functions.

In addition to first-class functions, another key concept in functional programming is the use of immutable data. This means that once data is created, it cannot be changed. Instead, any changes to the data must create a new copy of the data with the desired changes. This can be done in JavaScript using the Object.freeze method, which prevents an object from being modified.

To illustrate the use of immutable data in JavaScript, consider the following example:

// Define an object with some initial data
const data = {
  name: "John Doe",
  age: 45
};

// Freeze the object to prevent modification
Object.freeze(data);

// Attempt to change the value of the "name" property
data.name = "Jane Doe";  // This will have no effect

// Print the value of the "name" property
console.log(data.name);  // Output: "John Doe"

In this example, the data object is created with some initial values, and then it is frozen using the Object.freeze method. This prevents the object from being modified. However, if we try to change the value of the name property, it will have no effect, because the object is frozen.

By using first-class functions and immutable data, it is possible to write JavaScript code in a functional style that is easy to reason about and that is easy to test. This can be especially useful when writing code that is concurrent or parallelizable, because it can help to avoid race conditions and other synchronization issues.

Summary

In summary, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. JavaScript is a popular programming language that has many of the features associated with functional programming languages, making it possible to write JavaScript code in a functional style. By using first-class functions and immutable data, it is possible to write JavaScript code that is easy to reason about and that is easy to test, and that is well-suited for concurrent and parallelizable applications.