# Exploring Functional Programming in JavaScript

Contents: Click for quick access

## Introduction:

Functional programming is a popular way of writing code that focuses on using mathematical functions for computation and avoids altering data as much as possible. JavaScript, a flexible and powerful language, supports this approach, enabling developers to create clean, short, and easier-to-manage code. In this blog, we will explore functional programming concepts and learn how to apply them in JavaScript with practical examples.

## 1. Pure Functions:

Functional programming is built on the idea of pure functions. A pure function is a type of function that consistently gives the same result when given the same input, and it doesn’t cause any changes outside of the function. In other words, it doesn’t mess with any external stuff or variables. Let’s take a look at an example:

``````// Impure Function
let total = 0;
total += number;
}

// Pure Function
return a + b;
}``````

## 2. Immutability:

Functional programming encourages immutability, which means once data is created, it cannot be changed. Instead of directly modifying existing data, new data is created with the desired changes. JavaScript offers different tools and libraries to make it easier to work with immutable data. Here’s an example:

``````// Without Immutability
let arr = [1, 2, 3];
arr.push(4); // Modifies the original array

// With Immutability
let newArr = [...arr, 4]; // Creates a new array with the added element``````

## 3. Higher-order Functions:

Higher-order functions are special functions that can do two things: they can take one or more functions as inputs, and they can also return a function as their output. These functions are essential in functional programming because they allow us to combine functions together, making our code more flexible and powerful. Here’s an example of a higher-order function:

``````// Higher-order function
function doMath(operation, a, b) {
return operation(a, b);
}

return a + b;
}

function multiply(a, b) {
return a * b;
}

let result1 = doMath(add, 2, 3);       // Output: 5
let result2 = doMath(multiply, 2, 3);  // Output: 6``````

## 4. Map, Filter, and Reduce:

Map, filter, and reduce are three useful array methods in JavaScript that make functional programming easier. They allow us to perform various operations on arrays in a more functional and concise way.

``````// Map: Transforms elements of an array
let numbers = [1, 2, 3];
let doubled = numbers.map(num => num * 2); // Output: [2, 4, 6]

// Filter: Selects elements based on a condition
let evenNumbers = numbers.filter(num => num % 2 === 0); // Output: [2]

// Reduce: Reduces array elements to a single value
let sum = numbers.reduce((acc, curr) => acc + curr, 0); // Output: 6``````

## 5. Currying:

Currying is a technique in which we take a function that normally accepts multiple arguments and turn it into a sequence of functions, each taking only one argument. This approach is handy because it allows us to create functions that are more flexible, reusable, and easier to combine with other functions.

``````// Without currying
return a + b;
}

// With currying
return function(b) {
return a + b;
}
}