Functional programming is a programming paradigm designed to handle pure mathematical functions. This paradigm is totally focused on writing more compounded and pure functions. 1. Pure Functions: In functional programming, pure functions are essential. They take input and produce output without any side effects or reliance on external state. Pure functions always return the same output for the same input, making them predictable and easy to reason about.
``````
// Example of a pure function
return a + b;
}
```
```
2. Immutability: Functional programming promotes immutability, where data is not modified once created. Instead, new data is created when transformations are applied. Immutable data ensures code eliminates bugs related to unwanted side effects.
``````
// Example of immutability
const numbers = [1, 2, 3, 4, 5];

// Creating a new array by doubling each element
const doubledNumbers = numbers.map((num) => num * 2);

console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
console.log(numbers); // Output: [1, 2, 3, 4, 5] (original array remains unchanged)
```
```
3. Higher-Order Functions: Higher order functions are functions that take one or more functions as arguments, or return a function as their result.
``````
// Example of a higher-order function
function multiplyBy(factor) {
return function (number) {
return number * factor;
};
}

const multiplyByTwo = multiplyBy(2);
console.log(multiplyByTwo(5)); // Output: 10
```
```
4. Function Composition: Functional programming emphasizes function composition, where functions are combined to create new functions. This allows for building complex functionality by chaining simple, reusable functions together.
``````
// Example of function composition
return a + b;
}

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