Closures Advanced in JavaScript
Keep separate visitor counts in JavaScript with closures returned by createCounter. Methods share private local state; the demo still allows negative counts.
Run this example once in a fresh browser console. The console shows the printed results.
1function createCounter(label) {2 let count = 0;3 return {4 increment() { count++; return label + ': ' + count; },5 decrement() { count--; return label + ': ' + count; },6 value() { return count; }7 };8}9const library = createCounter('Library visitors');10const lab = createCounter('Lab visitors');11console.log(library.increment());12console.log(library.increment());13console.log(library.decrement());14console.log('Lab count:', lab.value());15console.log('Direct count:', library.count);
Sample Output
Library visitors: 1, then 2, then 1
The library methods all change the same remembered count.
Lab count: 0
A separate createCounter call made an independent variable.
Direct count: undefined
The returned object has no public count property.
In this one
- Keep state in an outer function
- Return methods that share that state
- Create independent counter instances
- Separate a local variable from an object property
How it works
- 1
Call createCounter to create a fresh count and label.
- 2
Return methods that remember those variables.
- 3
Increment the library counter twice, then decrement once.
- 4
Create a second counter without sharing the first count.
- 5
Print lab zero and direct count undefined to contrast remembered state with public properties.
Edge cases
Independent instances
Two calls must not accidentally share one global count.
Keep count inside createCounter. Each call creates a new remembered variable.
Negative count
The generic decrement method permits values below zero.
Add a domain rule if negatives are invalid. For attendance, you might return an error instead of decrementing zero.
What's going on
A closure is a function that remembers variables from the place where it was created. The remembered variables can stay available after the outer function finishes. This visitor-counter example gives each counter its own count without exposing that count as an ordinary object property.
createCounter creates count and returns three methods. Each method can still read or change that same count later. The first two library increments reach 2; decrement brings it back to 1. Creating the lab counter calls createCounter again, so it receives a separate remembered count starting at zero.
library.count is undefined because the returned object has no count property. The value method is the intended way to read it. This is useful for controlled local state, but it is not encryption or a place to hide a password from someone controlling the browser. The generic counter also allows negative values; an actual visitor tracker should choose and enforce a rule for decrementing below zero.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is lab.value after only library increments?
Pick an answer to check it.
Question 2
Which method intentionally reads the private count?
Pick an answer to check it.
Questions people ask
Why is count still available later?
The returned methods form closures over it. JavaScript keeps the variable available while those functions need it.
Why is library.count undefined?
count is a local variable, not a property placed on the returned object.
Do both counters share the same number?
No. Each createCounter call creates its own count variable.