Skip to main content

Closures


Closures is (a fancy term for) a function that remembers outside things that are used inside

const createPrinter() {
  const name = 'Alex'

  const printName = () => {
    console.log(name)
  }
  return printName
}

const myPrinter = createPrinter()

myPrinter() // Alex


This function that was return from from createPrinter function is called a closure. // Here is printName
Closure is a combination of a function and the environment where it was declared.

Comments