Functional Programming (FP) is a declarative programming paradigm where software is built by composing pure functions, avoiding shared mutable state, and eliminating side-effects.
// IMPURE FUNCTION (Depends on external mutable state & produces side-effects)
let taxRate = 0.18;
function calculateImpureTax(amount) {
taxRate = 0.20; // SIDE-EFFECT: Mutates outer state!
return amount * taxRate;
}
// PURE FUNCTION (Deterministic & Zero Side-Effects)
function calculatePureTax(amount, rate) {
return amount * rate; // Always returns same output for same inputs!
}
Currying converts a multi-argument function into a chain of single-argument functions using closures:
// Standard multi-arg function:
const add = (a, b, c) => a + b + c;
// Curried Version:
const curriedAdd = a => b => c => a + b + c;
console.log(curriedAdd(10)(20)(30)); // 60
// Practical Use: Specialized reusable discount calculator
const applyDiscount = discountPercent => price => price * (1 - discountPercent / 100);
const studentDiscount = applyDiscount(25); // Specialized function
console.log(studentDiscount(1000)); // 750
console.log(studentDiscount(2000)); // 1500
function memoize(fn) {
const cache = new Map();
return function(...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
console.log("Serving from Memoization Cache!");
return cache.get(key);
}
const result = fn(...args);
cache.set(key, result);
return result;
};
}
const expensiveFibonacci = memoize((n) => {
if (n <= 1) return n;
return expensiveFibonacci(n - 1) + expensiveFibonacci(n - 2);
});
console.log(expensiveFibonacci(40)); // Fast calculation with cache!
Exam Question: "What are the benefits of Functional Programming in modern JavaScript?"
How do you optimize runtime performance, prevent memory leaks, and debounce heavy events? Proceed to: JavaScript Performance Optimization & Memory Leaks →