Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

scope-closures-intro

Scope and closures intro

Last reviewed May 28, 2026 Content v20260528
Track mode
client_javascript
Means
In-browser JS
Reading
~1 min
Level
intermediate

This lesson

An orientation to this JavaScript lesson—scope, vocabulary, and what you will practice next.

You need a clear map of the JavaScript track so later lessons do not feel like isolated tricks.

You will apply Scope and closures intro in contexts like: Browsers, Node.js services, edge workers, and tooling ecosystems (bundlers, test runners).

Run JavaScript in the in-browser sandbox, use the terminal output panel, and verify with MCQs. Also open the interview prep blocks.

Start here at the beginning of the javascript track before skipping ahead.

Scope is where variables are visible. A closure is a function remembering variables from its creation environment.

Lexical scope

Inner functions see outer bindings—scope determined by source structure, not call stack alone.

Closure example

Counter factory returns a function that still sees let n after outer returns—used in modules and event handlers.

Important interview questions and answers

  1. Q: Closure use case?
    A: Private state, partial application, memoization.
  2. Q: Loop var bug?
    A: Classic var in loop + setTimeout—use let or IIFE.

Self-check

  1. Define closure in one sentence.
  2. Why let in loops with async?

Challenge

Closure counter

  1. Run the counter factory example.
  2. Call the returned function three times.
  3. Explain why count persists in a comment above printOutput.

Done when: you can explain closure in one sentence.

Interview prep

Closure?

Function plus remembered outer variables.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • What would you log to verify this behavior?
  • What breaks if you run this before the DOM is ready?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump