A pure function is a function that, given the same input, will always return the same output and has no side - effects. Side - effects include modifying global variables, making API calls, or changing the state of an object.
// Pure function
function add(a, b) {
return a + b;
}
const result = add(3, 5);
console.log(result); // Output: 8
Immutability means that once a data structure is created, it cannot be changed. Instead of modifying an existing object, new objects are created with the desired changes.
const originalArray = [1, 2, 3];
const newArray = [...originalArray, 4];
console.log(originalArray); // Output: [1, 2, 3]
console.log(newArray); // Output: [1, 2, 3, 4]
A higher - order function is a function that either takes one or more functions as arguments or returns a function.
function multiplyBy(factor) {
return function (number) {
return number * factor;
};
}
const double = multiplyBy(2);
console.log(double(5)); // Output: 10
The map
method creates a new array with the results of calling a provided function on every element in the calling array.
const numbers = [1, 2, 3];
const squaredNumbers = numbers.map(num => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9]
The filter
method creates a new array with all elements that pass the test implemented by the provided function.
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
The reduce
method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 10
Function composition is the process of combining two or more functions to produce a new function.
function addOne(num) {
return num + 1;
}
function doubleNum(num) {
return num * 2;
}
const composedFunction = num => doubleNum(addOne(num));
console.log(composedFunction(3)); // Output: 8
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument.
function add(a, b) {
return a + b;
}
const curriedAdd = a => b => a + b;
console.log(curriedAdd(3)(5)); // Output: 8
Functions should have a single responsibility. This makes the code easier to understand, test, and maintain.
// Good example
function getFullName(firstName, lastName) {
return `${firstName} ${lastName}`;
}
Minimize the use of side - effects in your functions. If you need to perform side - effects, isolate them in separate functions.
Use names that clearly describe what a function does or what a variable represents.
// Good example
function calculateTotalPrice(prices) {
return prices.reduce((total, price) => total + price, 0);
}
Functional programming in JavaScript offers a powerful set of tools and techniques to write more robust, maintainable, and modular code. By understanding fundamental concepts such as pure functions, immutability, and higher - order functions, and by applying common practices like function composition and currying, developers can take advantage of the benefits of functional programming. Following best practices, such as keeping functions small and focused and avoiding side - effects, will further enhance the quality of your code.