JS interview in 2 minutes / Closure
Tags: javascript, typescript, beginners, career
Published: 2021-05-17 09:49:56

---
title: JS interview in 2 minutes / Closure
published: true
description: Let's try to figure out how to answer common JS questions in an interview.
tags: javascript, typescript, beginners, career
cover_image: https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/1920px-Unofficial_JavaScript_logo_2.svg.png
---

**Question:**
Explain **closures** in JavaScript.


**Quick answer:**
It is a feature of JavaScript which allows you to access the context of the outer function from the inner function.

```javascript
function getCounter() {
    count = 0
    return function {
        count += 1
        return count
    }
}
```

**Longer answer:**
As we learned before, there are [Higher Order Functions](https://dev.to/kozlovzxc/js-interview-in-2-minutes-higher-order-functions-38kb) in JavaScript, which means you can create and return functions out of the functions. 

As we also know, functions can access each of their outer scopes.

```javascript
function a() {
    let variable = "hello world"
    function b() {
      console.log(variable)
    }
    b()
}
a() // hello world
```

So what are closures? Closures extend previous examples in a bit. They preserve all context for a given function when we leave the parent function.

```javascript
function a() {
    let variable = "hello world"
    function b() {
      console.log(variable)
    }
    return b
}

a()() //hello world
```

We have left the `a` function's scope during the run, but we still have reference to our `variable`. That is closure.

Closures even allow us to modify the enclosing variable.

```javascript
function generateCounter() {
    count = 0
    return function() {
        count++
        return count 
    }
}

counter1 = generateCounter()
counter2 = generateCounter()

counter1() // 1
counter1() // 2
counter1() // 3
// ❗️ This counter is different
counter2() // 1
```

**Real-life applications:**
Closures are the reason why [currying](https://dev.to/kozlovzxc/js-interview-in-2-minutes-currying-2hko) works in most cases.

```javascript
let sum2 = a => b => a + b // spot the closure 🔎
```

Also, closures can help you to [encapsulate](https://dev.to/scott_yeatts/comment/1e59h) some data from the public use.

```javascript
function Counter() {
    _count = 0
    
    function inc() {
        return ++_count
    }
    
    function dec() {
        return --_count
    }
    
    function count() {
        return _count
    }
    
    return { inc, dec, count }
}

let counter = Counter()
counter.inc() // 1
counter.inc() // 2
counter.dec() // 1
counter.count() // 1
counter._count = 1000
counter.count() // 1
// ^^^ Still 1, hack with _count haven't worked out
```

If you want to share more valuable cases, please add them as comments and I will add links to them to the post itself 🤝

**Resources:**
[MDN/Closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures)


**Other posts:**
* [JS interview in 2 minutes / Currying 🥘](https://dev.to/kozlovzxc/js-interview-in-2-minutes-currying-2hko)
* [JS interview in 2 minutes / Promise
](https://dev.to/kozlovzxc/js-interview-in-2-minutes-promise-4fhl)
* [JS interview in 2 minutes / this 🤯](https://dev.to/kozlovzxc/js-interview-in-2-minutes-this-3hlm)


---

Btw, I will post more fun stuff here and on [Twitter](https://twitter.com/kozlovzxc). Let's be friends 👋