Closures & Scope

Private State With Closures

Private State With Closures

A closure can keep state alive without putting that state in the global scope. Each time the outer function runs, it creates a fresh variable environment.

The returned function can update that private state on later calls. Separate returned functions should not share the same counter unless they came from the same outer call.

Your task: Complete makeCounter so it returns a function. Each call to the returned function should increase its private count by one and return the new count.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Private State With Closures

A closure can keep state alive without putting that state in the global scope. Each time the outer function runs, it creates a fresh variable environment.

The returned function can update that private state on later calls. Separate returned functions should not share the same counter unless they came from the same outer call.

Your task: Complete makeCounter so it returns a function. Each call to the returned function should increase its private count by one and return the new count.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.