Basics

Closures

A closure is the combination of a function and lexical environment which that function was declared. It executes with the scope that was defined in place, not with the state that's in place when it is executed. A function with lexical scope is able to access variables that were defined in the parent scope.

const prepareBark = dog => {
const say = `${dog} barked!`
return () => {
console.log(say)
}
}
​
const rogerBark = prepareBark(`Roger`)
const sydBark = prepareBark(`Syd`)
​
rogerBark() // Roger barked!
sydBark() // Syd barked

When we redefine a new say variable, it doesn't change the state of the first scope of prepareBark()

Why would you use one?

References