Question Bank

JavaScript Closures and Private State Quiz

Difficulty: Medium

Build the classic closure patterns: encapsulated counters, lexical-scope inheritance through nested functions, and constructor-level private fields.

Question Bank
/

JavaScript Closures and Private State Quiz

JavaScript Closures and Private State Quiz

Build the classic closure patterns: encapsulated counters, lexical-scope inheritance through nested functions, and constructor-level private fields.

Question Bank
Medium
JavaScript
4 questions
quiz
closures
js-lexical-scope
interview-prep

417 views

12

Implement createCounter() so the returned object exposes increment, decrement, and getCount while keeping count private (untouchable from the outside).

Examples

Example 1:

Input: const c = createCounter(); c.increment(); c.increment(); c.decrement(); c.getCount()
Output: 1
Explanation: `count` lives in the closure scope of `createCounter`; only the returned methods can reach it.