Skip to content
Justice Tech School

Closures

A closure is a function that keeps access to the variables around where it was created, even after the outer function has finished. This lets you keep private state.

Example
function makeCounter() {
  let count = 0;
  return () => {
    count++;
    return count;
  };
}
const a = makeCounter();
const b = makeCounter();
console.log(a(), a(), a());
console.log(b());

Exercise

Return the inner function.

function outer() {
  let n = 0;
   () => ++n;
}

Quiz Test

What does a closure remember?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.