Jump to content

Featured Replies

Posted

You are reading Part 25 of the 39-part series: JavaScript Skill Progression: The Path from Beginner to Extreme. [Level 4]

Introduction

Functional programming (FP) is a programming paradigm that emphasizes pure functions, immutability, and higher-order functions. JavaScript supports functional programming through features like first-class functions, closures, and built-in array methods.

1. Higher-Order JavaScript Functions

Higher-order functions are functions that take other functions as arguments or return functions as their result.

Example: A Function Taking Another Function as an Argument

function applyOperation(a, b, operation) {
    return operation(a, b);
}

function add(x, y) {
    return x + y;
}

console.log(applyOperation(5, 3, add)); // Output: 8

Example: A Function Returning Another Function

function multiplier(factor) {
    return function(number) {
        return number * factor;
    };
}

const double = multiplier(2);
console.log(double(4)); // Output: 8

2. Pure JavaScript Functions & Immutability

A pure function is a function that:

  • Produces the same output for the same input.

  • Has no side effects (does not modify external state).

Example of a Pure Function:

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

Example of an Impure Function (Modifying Global State):

let total = 0;
function addToTotal(amount) {
    total += amount;
}

Immutability Example:

const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4]; // Creates a new array instead of modifying the original
console.log(newNumbers); // Output: [1, 2, 3, 4]

3. JavaScript Closures & Lexical Scope

Closures allow a function to remember variables from its outer scope even after the outer function has executed.

Example of a Closure:

function counter() {
    let count = 0;
    return function() {
        count++;
        console.log(count);
    };
}

const increment = counter();
increment(); // Output: 1
increment(); // Output: 2

4. JavaScript Array Methods

JavaScript provides functional methods for array manipulation: map(), filter(), reduce(), forEach(), and find().

map() - Transform Each Element

const numbers = [1, 2, 3, 4];
const squared = numbers.map(n => n * n);
console.log(squared); // Output: [1, 4, 9, 16]

filter() - Select Elements Based on a Condition

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

reduce() - Accumulate Values

const sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum); // Output: 10

forEach() - Execute a Function for Each Element

numbers.forEach(num => console.log(num));

find() - Find the First Matching Element

const firstEven = numbers.find(n => n % 2 === 0);
console.log(firstEven); // Output: 2

5. Recursion in JavaScript

Recursion is a technique where a function calls itself to solve a problem.

Example: Factorial Function Using Recursion

function factorial(n) {
    if (n === 0) return 1;
    return n * factorial(n - 1);
}

console.log(factorial(5)); // Output: 120

Example: Fibonacci Sequence Using Recursion

function fibonacci(n) {
    if (n <= 1) return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

console.log(fibonacci(6)); // Output: 8

Functional programming principles improve code maintainability, reduce bugs, and enhance performance. Understanding higher-order functions, pure functions, closures, array methods, and recursion will help you write more efficient and readable JavaScript code.

You are reading Part 25 of the 39-part series: JavaScript Skill Progression: The Path from Beginner to Extreme. [Level 4]

  • Views 74
  • Created
  • Last Reply

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Important Information

Terms of Use Privacy Policy Guidelines We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.