What is Closure in JavaScript

What is Closure in JavaScript

In this article, we will learn another most important concepts of javascript, which is closure.

What is Closure in Javascript?

  A closure is the combination of a function and the lexical environment within which that function was declared.

When a function is a closure, it continues to have access to the scope of its outer functions even after the outer function has returned.

This means that even after a function has been completed, a closure can remember and access variables and arguments from the outer function.

In Simple words, closure is that is inner function can have access to the outer function variables/parameters as well as all the global variables.

For example,

 const outerFun = (a) => {
    let b = "Jigar";
    const innerFun = () => {
      console.log(a + b);
    };
    return innerFun;
  };

  let inner = outerFun("Hello..");
  inner();

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment