Back to JavaScript Course

JavaScript Course

Whenever you create a function within another function, you have created a closure. The inner function is the closure. This closure is usually returned so you can use the outer function's variables at a later time.

function outerFunction () { const outer = `I see the outer variable!` function innerFunction() { console.log(outer) } return innerFunction } outerFunction()() // I see the outer variable!

Since the inner function is returned, you can also shorten the code a little by writing a return statement while declaring the function.

function outerFunction () { const outer = `I see the outer variable!` return function innerFunction() { console.log(outer) } } outerFunction()() // I see the outer variable!

Since closures have access to the variables in the outer function, they are usually used for two things:

  • To control side effects
  • To create private variables

Controlling side effects with closures

Side effects happen when you do something in aside from returning a value from a function. Many things can be side effects, like an Ajax request, a timeout or even a console.log statement:

function (x) { console.log('A console.log is a side effect!') }

When you use closures to control side effects, you're usually concerned with ones that can mess up your code flow like Ajax or timeouts.

Lets go through this with an example to make things clearer.

Lets say you want to make a cake for your friend's birthday. This cake would take a second to make, so you wrote a function that logs made a cake after one second.

function makeCake() { setTimeout(_ => console.log(`Made a cake`), 1000) }

As you can see, this cake making function has a side effect: a timeout.

That's how closures are used to reduce side effects – you create a function that activates the inner closure at your whim.

Private variables with closures

As you know by now, variables created in a function cannot be accessed outside the function. Since they can't be accessed, they are also called private variables.

However, sometimes you need to access such a private variable. You can do so with the help of closures.

function secret (secretCode) { return { saySecretCode () { console.log(secretCode) } } } const theSecret = secret('Praveen Oruganti Rocks!!') theSecret.saySecretCode() // 'Praveen Oruganti Rocks!!'

saySecretCode in this example above is the only function (a closure) that exposes the secretCode outside the original secret function. As such, it is also called a privileged function.

Note

Generally, closures are used for data privacy.

Summary

When you declare a variable in a function, you can only access it in the function. These variables are said to be scoped to the function.

If you define any inner function within another function, this inner function is called a closure. It retains access to the variables created in the outer function.

You can also check out the Demo.