If you find this post useful, you can follow me on twitter, sign up to my mailing list or check out the other posts on my blog. I've also got a couple of active side projects that you might like to check out:
- ippy.io - An app for creating beautiful resumes
- many.tools - A collection of useful utilities for designers and devs
Closures are one of the classic âgotchasâ in JavaScript. There are countless articles across the internet describing closures as something you absolutely need to understand to consider yourself a competent developer, or must know before your next job interview, etc etc.
Donât get me wrong, understanding closures is very important. The thing is, I think thereâs a reasonable chance you already understand them, but just donât understand that you understand them đ. But if you donât, hopefully you will soon.
Itâs just a hunch, but my guess is that a fair bit of the confusion around closures is simply due to terminology. That is, it can take some time to connect unfamiliar words like âclosureâ and âlexical scopeâ with behaviour youâve observed and perhaps already understand in your code.
Letâs take a look at a relatively straightforward example to test your current understanding.
1. Counter
Take a look at the code below, and try to figure out the answers to the two commented questions (without running the code).
function createCounter() {
var count = 0
function getNext() {
count ++
return count
}
return getNext
}
console.log(count)
// ==> 1. What will this output?
const getNextNumber = createCounter()
const firstNumber = getNextNumber()
const secondNumber = getNextNumber()
const thirdNumber = getNextNumber()
const fourthNumber = getNextNumber()
console.log(
firstNumber,
secondNumber,
thirdNumber,
fourthNumber
)
// ==> 2. What will this output?
If you answered:
- ReferenceError (or if you knew this would be some kind of error)
- 1, 2, 3, 4
Congratulations! You understand closures!
There are two things you need to grasp from the code above:
- The
count
variable is not accessible anywhere outside of thecreateCounter()
function. - The
count
variable is accessible to any functions that are declared within thecreateCounter()
function (where it was initially declared).
This is all that a closure is. Using a function (in our case createCounter()
) to close over a variable.
There is no way for the count
variable to be accessed or set from anywhere else in our code, except via the function that we define and return from createCounter()
, the getNext()
function.
As you can see, getNext()
(since it was declared inside createCounter()
) maintains access to the count
variable, and is able to increment and return it.
Letâs take a look at a slightly more complex example.
2. Election Day
Imagine weâve been tasked with running an election. Itâs a somewhat odd election, as voters will be casting their ballots from our JavaScript console.
We want a way to:
- Keep track of the votes
- Allow people to cast votes
- Retrieve the final results (in a secure, password protected way)
We could do something like this (but shouldnât):
var candidateOneVoteCount = 0
var candidateTwoVoteCount = 0
function voteForCandidateOne() {
candidateOneVoteCount ++
}
function voteForCandidateTwo() {
candidateTwoVoteCount ++
}
function getResults(inputPassword) {
if (inputPassword !== "password123") {
throw new Error("Wrong password")
}
return {
candidateOne: candidateOneVoteCount,
candidateTwo: candidateTwoVoteCount
}
}
Since the variables storing the candidates votes are defined in the global scope, anyone casting their vote could sneakily rig our election simply by running candidateTwoVoteCount = 1000000
.
We need to keep our vote counts private. We only want it to be possible to change or retrieve these variables via the interface that weâve defined. That is, via:
voteForCandidateOne()
-
voteForCandidateTwo()
getResults()
How can we achieve this? With a closure. Letâs refactor the code above to use a closure.
function createElection(password) {
var candidateOneVoteCount = 0
var candidateTwoVoteCount = 0
function voteForCandidateOne() {
candidateOneVoteCount ++
}
function voteForCandidateTwo() {
candidateTwoVoteCount ++
}
function getResults(inputPassword) {
if (inputPassword !== password) {
throw new Error("Wrong password")
}
return {
candidateOne: candidateOneVoteCount,
candidateTwo: candidateTwoVoteCount
}
}
return {
voteForCandidateOne,
voteForCandidateTwo,
getResults
}
}
const {
voteForCandidateOne,
voteForCandidateTwo,
getResults
} = createElection("password123")
console.log(candidateOneVoteCount)
// ReferenceError
console.log(candidateTwoVoteCount)
// ReferenceError
console.log(getResults("incorrectPassword"))
// Error: Wrong password
console.log(getResults("password123"))
// => { candidateOne: 0, candidateTwo: 0 }
voteForCandidateOne()
voteForCandidateOne()
voteForCandidateTwo()
console.log(getResults("password123"))
// => { candidateOne: 2, candidateTwo: 1 }
// Please never run a real election using code like this.
Our interface functions voteForCandidateOne()
, voteForCandidateTwo()
, getResults()
are now declared within, and returned from createElection()
. As they are declared in the same scope, they maintain access to the variables storing the vote count (candidateOneVoteCount
& candidateTwoVoteCount
).
Itâs also worth noting that the functions also maintain access to the password
argument that is provided when createElection()
is called. This is later compared to the password provided in getResults()
to validate access.
Let me know if anything here is unclear, and I'll do my best to explain it further! đ»
Top comments (6)
I dont get in the first example why calling getNextNumber() increase variable count... I thought it would return 1,1,1,1... doesnt calling getNextNumber run createCounter() that set back count everytime to 0 does it?
Hey! Thanks for reading đ
What you're expecting would the case if instead of:
I'd written:
However, as the
createCounter
function is invoked when declaring the constgetNextNumber
, the return value ofcreateCounter()
is assigned togetNextNumber
, rather than the function itself.Since the
getNext
function is returned fromcreateCounter
, it is this function that's assigned to the constgetNextNumber
.So when we call
getNextNumber()
, it is really thegetNext
function that's being run multiple times, notcreateCounter
, which is only called a single time.Does that make sense?
Yes, after reading your explanation I get where I was failing. You are assigning getNextNumber variable the getNext() function, not the return of it, or for better day you are assigning the return of createCount() function which is in fact getNext() function. Thanks to pointing that for it to become as I thought, it need to be assined just as createCounter (no brackets) instaed of createCounter(). Hope may help somebody like me who failed to grasp it đ. Thanks again
Actually with createElection() you've effectively created an object. You could access it like this:
const obj = createElection()
Hey hey, thanks for reading đ»
createElection()
does indeed return an object. Which is why this line works to destructure the object into const values:What's important here is that the returned object doesn't expose the vars
candidateOneVoteCount
&candidateTwoVoteCount
directly - these are only accessible via the returned functions in that object. For example:Similarly, you're not able to overwrite those variables by setting properties on the returned object:
Its an example of functional programming being able to represent OO programing (as in the SICP text book). Even if Javascript didn't support objects, using closures and currying can give you "objects" with pure functional constructs.