Scott Whittaker

Frontend Developer

Day job React | side projects Svelte

JavaScript Closures

You can guarantee you will be asked about closures in an interview and if you are anything like me then you know what they are and how to use them (as you use them every day) but you forgot or cannot articulate it back to the interviewer. Here are a few succinct definitions to commit to memory…

Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure ‘remembers’ the environment in which it was created.

MDN

In a nutshell, a closure is the combination of a function bundled together (enclosed) with references to it’s surrounding state (the lexical environment). In JavaScript, closures are created every time a function is created, at function creation time.

Eric Elliot

Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.

Kyle Simpson