Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Functional programming treats computation as evaluating mathematical functions. Focus on what to do, not how to do it. Use pure functions, avoid mutations, and compose small functions!
let total = 0;
// Impure - modifies external state
function add(value) {
total += value;
return total;
}
add(5); // 5
add(5); // 10 (different result!)
// Impure - depends on external state
function getTax() {
return total * 0.1;
}
// Impure - random output
function getRandomPrice() {
return Math.random() * 100;
}
// Impure - mutates input
function addToArray(arr, item) {
arr.push(item);
return arr;
}// Pure - takes all inputs as parameters
function add(a, b) {
return a + b;
}
add(5, 10); // 15
add(5, 10); // 15 (always same!)
// Pure - all inputs explicit
function getTax(amount) {
return amount * 0.1;
}
// Pure - deterministic
function getPrice(basePrice, quantity) {
return basePrice * quantity;
}
// Pure - returns new array
function addToArray(arr, item) {
return [...arr, item];
}Easy to test, cache, and reason about
const user = {
name: 'Alice',
age: 25
};
// Mutates original object
user.age = 26;
console.log(user); // { name: 'Alice', age: 26 }
const numbers = [1, 2, 3];
// Mutates original array
numbers.push(4);
console.log(numbers); // [1, 2, 3, 4]
// Hard to track changes
function updateUser(user) {
user.lastModified = Date.now();
return user; // Modified original!
}const user = {
name: 'Alice',
age: 25
};
// Creates new object
const updated = { ...user, age: 26 };
console.log(user); // { name: 'Alice', age: 25 }
console.log(updated); // { name: 'Alice', age: 26 }
const numbers = [1, 2, 3];
// Creates new array
const newNumbers = [...numbers, 4];
console.log(numbers); // [1, 2, 3]
console.log(newNumbers); // [1, 2, 3, 4]
// Easy to track
function updateUser(user) {
return { ...user, lastModified: Date.now() };
}Array methods that don't mutate
// Function that returns a function
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
const double = multiplier(2);
const triple = multiplier(3);
console.log(double(5)); // 10
console.log(triple(5)); // 15
// Function that takes a function
function applyOperation(arr, operation) {
return arr.map(operation);
}
const numbers = [1, 2, 3, 4, 5];
const squared = applyOperation(numbers, x => x * x);
console.log(squared); // [1, 4, 9, 16, 25]
// Combining both
function createLogger(prefix) {
return function(message) {
console.log(`[${prefix}] ${message}`);
};
}
const errorLog = createLogger('ERROR');
const infoLog = createLogger('INFO');
errorLog('Something went wrong'); // [ERROR] Something went wrong
infoLog('Process completed'); // [INFO] Process completedFocus on what, not how
Functional approach to state management
Same input = same output
No side effects, easy to test
Never modify, always create new
Use map, filter, reduce, spread
Functions as arguments/returns
Enable powerful abstractions
Express what, not how
Readable, maintainable code