Functional Programming in Javascript

Capítulo 107

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Functional programming is a programming paradigm that treats computation as an evaluation of mathematical functions and avoids state change and mutable data. In JavaScript, this is made easier by the fact that functions are first-class objects, meaning they can be passed as arguments to other functions, returned as values ​​from other functions, and assigned to variables.

Functional programming in JavaScript generally involves the use of pure functions, which are functions that give the same result for the same arguments and have no side effects. This means that they do not modify any state or variable outside the function. This makes its behavior predictable and easy to test.

For example, consider the following pure function:

function sum(a, b) {
    return a + b;
}

This function will always return the same result for the same arguments and will not modify any external state.

Another important concept in functional programming is immutability, which is the idea that once a variable is created, its value cannot be changed. Instead, any modification to a variable will result in a new variable. This is useful to avoid side effects and make the code more predictable.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

let a = 1;
let b = a + 1; // b is now 2, but a is still 1

High-order functions are another pillar of functional programming. These are functions that operate on other functions, either taking them as arguments or returning them as a result. In JavaScript, high-order functions are commonly used with array methods such as map, filter, and reduce.

let numbers = [1, 2, 3, 4, 5];

let doubled = numbers.map(function(n) {
    return n * 2;
}); // [2, 4, 6, 8, 10]

let pairs = numbers.filter(function(n) {
    return n % 2 === 0;
}); // [2, 4]

let sum = numbers.reduce(function(total, n) {
    return total + n;
}, 0); // 15

Using higher order functions like these can make code more concise and readable.

Finally, function composition is another common technique in functional programming. This involves creating complex functions by combining simpler functions. In JavaScript, this can be done using the compose function, which can be implemented as:

function compose(f, g) {
    return function(x) {
        return f(g(x));
    };
}

With this function, you can create new functions by combining other functions. For example, you could create a function to double and then add 1 to a number:

let foldEAdd1 = compose(function(n) { return n + 1; }, function(n) { return n * 2; });

foldAndAdd1(4); // 9

In summary, functional programming is a powerful paradigm that can make code more predictable, easier to test, and easier to understand. While it may be a little different than what you're used to if you come from an imperative or object-oriented background, it's worth learning and using in your JavaScript code.

Now answer the exercise about the content:

Which of the following concepts is NOT a pillar of functional programming in JavaScript?

You are right! Congratulations, now go to the next page

You missed! Try again.

Class inheritance is a concept related to object-oriented programming, not functional programming. Functional programming in JavaScript emphasizes immutability, high-order functions, and function composition, among other principles, rather than using class-based inheritance.

Next chapter

Reactive Programming with RxJS

Arrow Right Icon
Free Ebook cover Complete HTML, CSS and Javascript course to become a Front End Developer
86%

Complete HTML, CSS and Javascript course to become a Front End Developer

3.79

(14)

125 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.