Subjects › JavaScript

Functions and closures

Scope, closures, and what `this` refers to.

The idea

A closure is just a function that remembers where it was defined. The classic loop-variable bug is the best teaching example because the fix (let instead of var) is one word and the reason is structural.

Lesson

No lesson built yet. The worked solutions below are the material it would be assembled from.

Questions (3)

Question Type Difficulty Drift Accuracy
What does this log? for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } mcq Moderate 0.30 — — (0)
What does this log? function counter() { let n = 0; return () => ++n; } const c = counter(); c(); c(); co… numeric Hard 0.70 — — (0)
Explain why an arrow function and a regular function differ in what `this` refers to, and when that difference… short Hard 1.40 — — (0)