Mastering Closures in JavaScript: A Beginner’s Guide

Closures are a fundamental concept in JavaScript, and they can be a little tricky to understand at first. But once you get the hang of them, they …


Mastering Closures in JavaScript: A Beginner’s Guide

Closures are a fundamental concept in JavaScript, and they can be a little tricky to understand at first. But once you get the hang of them, they can be a powerful tool in your JavaScript toolkit.

So, what is a closure? Simply put, a closure is a function that remembers the environment in which it was created, even when it is executed outside of that environment.

To understand this concept better, let’s take a look at an example. Consider the following code:

function outerFunction(param) {
  let outerVariable = 'I am the outer variable';

  function innerFunction() {
    console.log(outerVariable + ', and the param is ' + param);
  }

  return innerFunction;
}

let returnedFunction = outerFunction('Hello, World!');
returnedFunction();

In this code, we have an outer function called outerFunction and an inner function called innerFunction. The outerFunction takes a parameter called param, and it also has a local variable called outerVariable. The innerFunction has no parameters and no local variables, but it does reference the outerVariable and the param of the outerFunction.

When we call outerFunction with the argument 'Hello, World!', it creates a new execution context with its own local variables and arguments. In this case, param is set to 'Hello, World!' and outerVariable is set to 'I am the outer variable'.

Then, the outerFunction returns the innerFunction. However, when we execute the innerFunction outside of the outerFunction, it still has access to the local variables and arguments of the outerFunction. This is because the innerFunction has a closure over the outerFunction‘s execution context.

So, when we call returnedFunction(), it logs 'I am the outer variable, and the param is Hello, World!' to the console.

Closures can be a little tricky to wrap your head around at first, but they can be very useful in certain situations. For example, you might use a closure to create a private variable that can only be accessed by a specific function. Or, you might use a closure to create a function that has access to some external state that it needs to use when it is called.

In summary, a closure is a function that remembers the environment in which it was created, and it can be a very useful tool in JavaScript programming.

Have any question, suggestion or comment?

Evren Bal

I am Evren BAL

I've been an ‘Internet Creature’ since 1996!

More info about About Evren BAL

If you want to meet one-on-one, feel free to contact me via social media.

Reach Me Out

Reach Me Out

  • Have a question?
  • Noticed a mistake in the article?
  • Have a suggestion about the page?
  • Have a suggestion for an aticle topic?

Reach me out using the contact form or via my social media accounts.

Digital Ocean Logo

Want to try a free VPS?

Using my referral link, you may create a new Digital Ocean account with $100 credit valid for 60 days. This gives you the opportunity to try even the highest performing VPS.

My advice will be to start with a VPS that is feasible and affordable to use after the trial, so that the time you spend on setting up the server is not wasted.

Get your free credits here

You don't have to pay any penny, if you don't want to continue at the end of 60-day trial. If you prefer to continue with the paid service, your first $25 order will reward me $25 free credit that I can use for my hosting

In other words, you will get 100$ credit anyway, and I'll get a 25$ credit if you continue with the paid service in the long run.

Copyright © 2023 - Evren BAL