Fireship · @fireship

Closures Explained in 100 Seconds // Tricky JavaScript Interview Prep

Uploaded June 17, 2021 · Length 4:57

Closures Explained in 100 Seconds // Tricky JavaScript Interview Prep

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.

  1. 0:00

    javascript closures are functions that

  2. 0:02

    can access values outside of their own

  3. 0:04

    curly braces in order to call a function

  4. 0:07

    in your code the javascript interpreter

  5. 0:09

    needs to know about the function itself

  6. 0:11

    and any other data from the surrounding

  7. 0:12

    environment that it depends on

  8. 0:14

    everything needs to be neatly closed up

  9. 0:16

    into a box before it can be fed into the

  10. 0:18

    machine

  11. 0:18

    take for example a pure function that

  12. 0:20

    only depends on its own arguments and

  13. 0:22

    internal data

  14. 0:23

    what we have here is a fully

  15. 0:25

    self-contained closed expression

  16. 0:27

    when it's called it gets pushed onto the

  17. 0:28

    call stack where it's executed and its

  18. 0:31

    internal data is only kept in memory

  19. 0:32

    until it's popped back off the call

  20. 0:34

    stack but what if that function

  21. 0:35

    references data

  22. 0:36

    outside of its own scope like from the

  23. 0:38

    global environment or an outer function

  24. 0:40

    that leaves us with an open expression

  25. 0:42

    that references other

  26. 0:44

    free variables throughout the

  27. 0:45

    environment now in order for the

  28. 0:46

    interpreter to call this function

  29. 0:48

    and also know the value of these free

  30. 0:50

    variables it creates a closure to store

  31. 0:52

    them in a place in memory where they can

  32. 0:54

    be accessed later

  33. 0:55

    that area of memory is called the heap

  34. 0:57

    and unlike the call stack which is

  35. 0:58

    short-lived

  36. 0:59

    it can keep data in memory indefinitely

  37. 1:01

    then decide once you get rid of it later

  38. 1:03

    with the garbage collector

  39. 1:04

    so a closure is not just a function it's

  40. 1:06

    a function combined with its outer state

  41. 1:09

    or lexical environment as you might

  42. 1:10

    imagine closures require more memory and

  43. 1:12

    processing power than a pure function

  44. 1:14

    but you'll come across many practical

  45. 1:16

    reasons to use them

  46. 1:17

    the most important one being data

  47. 1:18

    encapsulation to prevent leaking or

  48. 1:20

    exposing data where it's not needed we

  49. 1:22

    can create a closure by defining an

  50. 1:24

    outer function that contains the state

  51. 1:26

    then an inner function that operates on

  52. 1:28

    it the data contained here will not leak

  53. 1:30

    out to the surrounding environment the

  54. 1:31

    inner function has access to data

  55. 1:33

    defined in the outer function scope but

  56. 1:35

    the outer function does not have

  57. 1:37

    access to the inner function in addition

  58. 1:39

    many javascript apis are callback based

  59. 1:41

    you can use closures to create a

  60. 1:43

    function factory that takes an argument

  61. 1:45

    then returns a brand new function which

  62. 1:47

    can then be passed along to other

  63. 1:48

    functions that expect a callback

  64. 1:50

    this has been javascript closures in 100

  65. 1:52

    seconds but stay tuned

  66. 1:54

    because today we're going beyond 100

  67. 1:55

    seconds to take a look at one of the

  68. 1:57

    most famous javascript trick questions

  69. 1:59

    that will wreck you on an interview if