Closure in JavaScript

samrat9x

Samrat

Posted on August 31, 2023

Closure in JavaScript

Closure is nothing but a technique bringing out the variable to the outer scope so that parent scope can also work with the variable from child scope.

function foo(){
    let count = 0;
    return function(){
        return count+=1;
    }}

let doo = foo();

console.log(doo());
console.log(doo());
console.log(doo());
console.log(count);
Enter fullscreen mode Exit fullscreen mode

Here count variable can be mutated from the outer scope.
But count variable cannot be accessed directly from outside the foo function.

💖 💪 🙅 🚩
samrat9x
Samrat

Posted on August 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Closure in JavaScript
closure Closure in JavaScript

August 31, 2023

Javascript Closures
javascript Javascript Closures

May 16, 2022