Mid JavaScript

What are JavaScript closures and how are they used?

A closure is created when a function remembers variables from its outer scope, even

after that outer function has finished executing.

Example:

function makeCounter() {

let count = 0;

return function() {

return ++count;

const counter = makeCounter();

console.log(counter()); // 1

console.log(counter()); // 2

✅ Use cases: data privacy, memoization, and function factories.

More from JavaScript Tutorial

All questions for this course