Programming foundations

Stage 1 · Lesson 5 of 10

Closures

Your first language

A is a function that remembers the variables from the place where it was created. It keeps them even after the outer function has finished. This works because of lexical : a function can see the variables written around it.

For example, function counter() { let n = 0; return () => ++n; } gives you a function with its own private n that goes up on each call. Closures are everywhere in JavaScript. , passed to map and filter, timers and React hooks all rely on them.

Common trapa loop that creates functions. With let, each pass gets its own variable. With the old var, all passes share one, so every callback sees the last value. Learning closures now, before any , is what the stage advice means by avoiding years of confusion.

A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

  • CoursePaid
    Grokking JavaScript Fundamentals

    Design Gurus · Beginner · $50

    The language itself, closures and async included, with runnable exercises per lesson

  • GuideFree
    JavaScript.info

    Ilya Kantor

    Free: Part 1 is the most complete modern JavaScript tutorial, with tasks per chapter

Practise

  • Small exercises in your language track, with free feedback from a human mentor

  • Foundations path: the JavaScript basics lessons and the exercises repo with tests

Keep handy

  • The JavaScript reference: look up every array and string method here

Mark this stage as not done?