Fireship · @fireship
Closures Explained in 100 Seconds // Tricky JavaScript Interview Prep
Uploaded June 17, 2021 · Length 4:57
Summary
javascript closures explained with examples, including how they capture variables and create a lexical environment.
- javascript closures
- lexical environment
- variable capture
- callback functions
- javascript interview prep
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
javascript closures are functions that
- 0:02
can access values outside of their own
- 0:04
curly braces in order to call a function
- 0:07
in your code the javascript interpreter
- 0:09
needs to know about the function itself
- 0:11
and any other data from the surrounding
- 0:12
environment that it depends on
- 0:14
everything needs to be neatly closed up
- 0:16
into a box before it can be fed into the
- 0:18
machine
- 0:18
take for example a pure function that
- 0:20
only depends on its own arguments and
- 0:22
internal data
- 0:23
what we have here is a fully
- 0:25
self-contained closed expression
- 0:27
when it's called it gets pushed onto the
- 0:28
call stack where it's executed and its
- 0:31
internal data is only kept in memory
- 0:32
until it's popped back off the call
- 0:34
stack but what if that function
- 0:35
references data
- 0:36
outside of its own scope like from the
- 0:38
global environment or an outer function
- 0:40
that leaves us with an open expression
- 0:42
that references other
- 0:44
free variables throughout the
- 0:45
environment now in order for the
- 0:46
interpreter to call this function
- 0:48
and also know the value of these free
- 0:50
variables it creates a closure to store
- 0:52
them in a place in memory where they can
- 0:54
be accessed later
- 0:55
that area of memory is called the heap
- 0:57
and unlike the call stack which is
- 0:58
short-lived
- 0:59
it can keep data in memory indefinitely
- 1:01
then decide once you get rid of it later
- 1:03
with the garbage collector
- 1:04
so a closure is not just a function it's
- 1:06
a function combined with its outer state
- 1:09
or lexical environment as you might
- 1:10
imagine closures require more memory and
- 1:12
processing power than a pure function
- 1:14
but you'll come across many practical
- 1:16
reasons to use them
- 1:17
the most important one being data
- 1:18
encapsulation to prevent leaking or
- 1:20
exposing data where it's not needed we
- 1:22
can create a closure by defining an
- 1:24
outer function that contains the state
- 1:26
then an inner function that operates on
- 1:28
it the data contained here will not leak
- 1:30
out to the surrounding environment the
- 1:31
inner function has access to data
- 1:33
defined in the outer function scope but
- 1:35
the outer function does not have
- 1:37
access to the inner function in addition
- 1:39
many javascript apis are callback based
- 1:41
you can use closures to create a
- 1:43
function factory that takes an argument
- 1:45
then returns a brand new function which
- 1:47
can then be passed along to other
- 1:48
functions that expect a callback
- 1:50
this has been javascript closures in 100
- 1:52
seconds but stay tuned
- 1:54
because today we're going beyond 100
- 1:55
seconds to take a look at one of the
- 1:57
most famous javascript trick questions
- 1:59
that will wreck you on an interview if


