Day 14 — Scope & closures

Lesson

Topic 1 Part 1
function makeCounter() {
    let count = 0;
    return function () {
        count++;
        return count;
    }
}

Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.

Section 2 Part 2
const cnt1 = makeCounter();
const cnt2 = makeCounter();

Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.

Section 3 Part 3
console.log(cnt1());
console.log(cnt1());

Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.

Section 4 Part 4
console.log(cnt1());
console.log(cnt1());

Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.

Section 5 Part 5
console.log(cnt2());
console.log(cnt2());

Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.

Section 6 Part 6
console.log(cnt1())

Code below mirrors ../js/lesson.js. Open DevTools → Console to see output when the script runs.