Question on real world use of closures

So, I’ve been trying to tackle the concept of closures and have been watching multiple people explain it. From my understanding it’s having access to the scope of a parent function after the parent function has already run and closed.

I’m somewhat understanding them and what they are, but I don’t understand why you would actually use them.

everything I’ve seen so far explains pretty well what they are, but no one seems to explain why you would use them.

I get confused on why someone wouldn’t do something like this.

let globalvariable = 'global'

function outerFunction() {
    let outerVariable = 'outer'

    function innerFunction() {
        let innerVariable = 'inner'
        console.log(`${globalvariable} ${outerVariable} ${innerVariable}`)
    }
    
    innerFunction()
}

outerFunction()

over something like this.

let globalvariable = 'global'

function outerFunction() {
    let outerVariable = 'outer'

    function innerFunction() {
        let innerVariable = 'inner'
        console.log(`${globalvariable} ${outerVariable} ${innerVariable}`)
    }
    
    return innerFunction
}

let innerFunct = outerFunction()
innerFunct()

Honestly, this is not the greatest challenge to explain why you would use closure with the example given.

Below is a more realistic use of closure.

function counter(counterName) {
    let count = 0;

    function counterInfo() {
        count++;
        console.log(`${counterName} - Count: ${count}`)
    }
    
    return counterInfo;
}
let counter1 = counter('Number1');
counter1(); // Number1 - Count: 1
counter1(); // Number1 - Count: 2

let counter2 = counter('Number 2');
counter2(); // Number2 - Count: 1
counter2(); // Number2 - Count: 2
counter2(); // Number2 - Count: 3

In this example, we want to be able to create multiple counters that keep track of how many times they have been called. The main function counter returns the inner function counterInfo. It is really counterInfo that gets called after initializing a variable with a call to counter. The count variable is accessible to the counterInfo function, so it can return its value when the intialized counter gets called.

1 Like

So, I rewrote your example into what I was using in the previous example.

function counter(counterName) {
    let count = 0;

    function counterInfo() {
        count++;
        console.log(`${counterName} - Count: ${count}`)
    }
    
    counterInfo()
}

counter('number1')
counter('number1')
counter('number2')
counter('number2')
counter('number2')

Now I think I can really see the difference. In the example I’m using it’s calling counter which is resetting the value of count back to 0 every time.

in your example it’s ONLY calling counterInfo and not counter and is able to get the value of count without resetting it back to 0

Am I correct in that?

Thank you, now that I have more of an understanding, I’m going to go over some more lessons and see if I can grasp it better!

I’ll take a look here shortly, thank you!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.