Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

JavaScript Essentials - Functional Programming

Introduction to functional programming concepts

Functional programming (FP) is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. This tutorial introduces you to the fundamental concepts of functional programming in JavaScript.

Key Points:

  • Functional programming emphasizes immutability and pure functions.
  • Higher-order functions and first-class functions are core concepts in FP.
  • FP promotes the use of declarative programming over imperative programming.

Pure Functions

Pure functions are functions that do not cause side effects and always return the same output for the same input. This predictability makes them easier to test and reason about.


// Example of a pure function
function add(a, b) {
    return a + b;
}

console.log(add(2, 3)); // Output: 5
console.log(add(2, 3)); // Output: 5 (always returns the same result for the same inputs)
                

Immutability

Immutability is a core principle in functional programming. It means that once a data structure is created, it cannot be changed. Instead, new data structures are created with the desired changes.


// Example of immutability
const arr = [1, 2, 3];
const newArr = arr.concat(4); // Creates a new array

console.log(arr); // Output: [1, 2, 3]
console.log(newArr); // Output: [1, 2, 3, 4]
                

Higher-Order Functions

Higher-order functions are functions that take other functions as arguments or return functions as their result. They are a key feature of functional programming.


// Example of higher-order functions
function multiplyBy(factor) {
    return function(number) {
        return number * factor;
    };
}

const double = multiplyBy(2);
console.log(double(5)); // Output: 10
                

First-Class Functions

In JavaScript, functions are first-class citizens, meaning they can be stored in variables, passed as arguments to other functions, and returned from functions.


// Example of first-class functions
const greet = function(name) {
    return 'Hello, ' + name;
};

function greetUser(greetFunction, userName) {
    return greetFunction(userName);
}

console.log(greetUser(greet, 'Alice')); // Output: Hello, Alice
                

Declarative vs Imperative Programming

Functional programming favors a declarative style of programming, where you describe what you want to achieve rather than how to achieve it.


// Imperative programming
const numbers = [1, 2, 3, 4, 5];
let doubled = [];
for (let i = 0; i < numbers.length; i++) {
    doubled.push(numbers[i] * 2);
}
console.log(doubled); // Output: [2, 4, 6, 8, 10]

// Declarative programming
const doubledDeclarative = numbers.map(num => num * 2);
console.log(doubledDeclarative); // Output: [2, 4, 6, 8, 10]
                

Common Functional Programming Methods

JavaScript provides several built-in methods that are commonly used in functional programming, including map, filter, and reduce.


// Using map
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]

// Using filter
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]

// Using reduce
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // Output: 15
                

Summary

In this tutorial, you learned about the fundamentals of functional programming in JavaScript, including pure functions, immutability, higher-order functions, first-class functions, and the difference between declarative and imperative programming. Embracing functional programming principles can lead to more predictable and maintainable code.