- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Closures
JavaScript Scope & Hoisting
JavaScript Closures
A JavaScript closure is a function that remembers the variables around it, even after the outer function has finished.
The inner function keeps a live link to the scope it was created in.
This is how a function can hold private state between calls.
Example
Example
javascript
function makeCounter() {
let count = 0;
return function () {
count++;
return count;
};
}
const counter = makeCounter();
console.log(counter());
console.log(counter());