# Functional Programming Concepts

Functional Programming (FP) is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that map values to other values, rather than a sequence of imperative statements which update the running state of the program.

## 1. First-Class Functions

In JavaScript, functions are treated as first-class citizens. This means functions can be:
*   Assigned to variables.
*   Passed as arguments to other functions.
*   Returned from other functions.

```javascript
const sayHello = function() {
  return "Hello!";
};
```

## 2. Pure Functions

A pure function is a function where the return value is determined only by its input values, without observable side effects.

*   **Deterministic:** Given the same input, it will always return the same output.
*   **No Side Effects:** It doesn't modify external state (variables, DOM, console, etc.).

```javascript
// Pure
function add(a, b) {
  return a + b;
};

// Impure (relies on external state)
let z = 10;
function addZ(x) {
  return x + z;
};
```

## 3. Immutability

Immutability means that once a data structure is created, it cannot be changed. Instead of modifying existing objects, you create new ones with the updated values.

```javascript
const person = { name: "John", age: 25 };

// Bad (Mutation)
person.age = 26;

// Good (Immutability)
const updatedPerson = { ...person, age: 26 };
```

## 4. Higher-Order Functions

A higher-order function is a function that takes one or more functions as arguments, or returns a function as its result. `map`, `filter`, and `reduce` are classic examples.

```javascript
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2); // map takes a function
```

## 5. Function Composition

Function composition is the process of combining two or more functions to produce a new function. Composing functions `f` and `g` produces `f(g(x))`.

[[programming/javascript/vanilla/javascript]]
[[programming/javascript/vanilla/map-filter-reduce]]
[[programming/javascript/vanilla/currying]]