In this post, we will see tutorial on javascript clusure

Closure :

  • A closure in JavaScript is a function that has access to variables in its outer scope, even after the outer function has returned. This allows the inner function to “remember” the values of the variables from its outer scope, even after the outer function has completed execution.

Closure Example in JavaScript:

function outerFunction(x) {
  return function innerFunction(y) {
    return x + y;

var add5 = outerFunction(5);
console.log(add5(3)); // 8

In this example, the outerFunction takes a single argument x and returns a function innerFunction that takes a single argument y. The innerFunction has access to the variable x from its outer scope, even after the outerFunction has returned. When add5 is assigned the result of outerFunction(5), it becomes a closure that “remembers” the value of x as 5. When add5 is called with an argument of 3, it returns the result of x + y, which is 8.

Closures are useful in many ways, including creating private variables, implementing function factories, and more. They are a fundamental concept in JavaScript and are used extensively in advanced JavaScript programming.

Thanks for Reading.