---
title: "Comment by Paarth Bajaj on \"Understanding \"Closures\" and Lexical Environment\" | Hashnode"
description: "Now if you combine all those functions (not one, but all) with each of their lexical environment, that is called Closure! Explain this in the code please."
url: "https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f"
---

[Discussion](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6)

Comment [Understanding "Closures" and Lexical Environment](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6)

[Paarth Bajaj](https://hashnode.com/@PaarthBajaj)

[Jul 30, 2021](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f)

Now if you combine all those functions (not one, but all) with each of their lexical environment, that is called Closure! Explain this in the code please.

[Mansoor Ameen](https://hashnode.com/@mansoorameen)

[Jul 30, 2021](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f#reply-6103fac3805a765b08876ba2)

If you see the code mentioned in this post on the top, that itself is a closure(the function b has access to var num that is outside the function b, which is also its lexical environment). What I meant by "combine all those functions (not one, but all)" is that, if you had many nested functions here like:

```
function a() {
    var num = 10;

    function b() {
        var newNum = num + 5;
        console.log(newNum)

        function c() {
            console.log(num)

            function d() {
                console.log(num)
            }
        }
    }
}
```

each of these functions has its own lexical environment, right? And this whole code is called closure\[ combine all the lexical environments, you get a closure\]. As it is closure, all these functions will have access to var num, and also any variables declared in the global scope.

I think that last line in my post is confusing. Will update it.

[Paarth Bajaj](https://hashnode.com/@PaarthBajaj)

[Jul 30, 2021](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f#reply-6103fdb79036db5b3513c8be)

Which one is a closure? function a or function b or function c or function d ? Which one ? If not function then from which part to which part, I should say that it is closure? [Mansoor Ameen](https://hashnode.com/@mansoorameen)

[Mansoor Ameen](https://hashnode.com/@mansoorameen)

[Jul 30, 2021](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f#reply-61040d1e805a765b08876dee)

[Paarth Bajaj](https://hashnode.com/@PaarthBajaj) Functions form closures. Here each function forms a closure, and that's why they can access var num.

[Paarth Bajaj](https://hashnode.com/@PaarthBajaj)

[Jul 30, 2021](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f#reply-610410b0805a765b08876f00)

Thank you for answering my stupid doubts! [Mansoor Ameen](https://hashnode.com/@mansoorameen)

[Mansoor Ameen](https://hashnode.com/@mansoorameen)

[Jul 30, 2021](https://hashnode.com/posts/understanding-closures-and-lexical-environment/60f78c673e181324dac717c6/comment/6103efbf9036db5b3513c69f#reply-6104184e805a765b0887702c)

[Paarth Bajaj](https://hashnode.com/@PaarthBajaj) Glad to be of some help :)
