DEV Community

Sathish Kumar
Sathish Kumar

Posted on

Bank() Function:

function bank() {

let balance = 500; // Private variable
function deposit(amt) {
    balance = balance + amt; // Accessing outer scope variable
    return balance;
}
return deposit;
Enter fullscreen mode Exit fullscreen mode

}
const depositFn = bank();

What happens here?

The bank() function holds an internal variable balance.

​The inner function deposit() updates and returns the balance.

​Even after bank() finishes executing, depositFn remembers the balance variable!

Top comments (0)