> For the complete documentation index, see [llms.txt](https://javascript-1.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascript-1.gitbook.io/javascript/fp/hof-and-closures.md).

# HOF and Closures

#### HOF:

A function that takes one or more functions as an arguments or returns a function as result.

```javascript
Ex 1:
const HOF = () => () => 5;
HOF()(); // 5

Ex 2:
const HOF2 = (fn) => fn(5);
HOF2((num) => {
    console.log(num);
}); // 5
```

#### Closure:

Closure allows the function to access the variables from the closing scope after leaving its scope in which it was declare.

```javascript
Ex:
const closure = function() {
    let count = 0;
    return function increment() {
        count++;
        return count;
    }
};

const incrementFn = closure();
incrementFn(); // 1
incrementFn(); // 2
incrementFn(); // 3
```
