1 00:00:00,790 --> 00:00:05,370 A closure is a function with its own private store of variables 2 00:00:05,370 --> 00:00:08,520 that no other function knows about or can access. 3 00:00:09,590 --> 00:00:12,190 You can create any number of closures. 4 00:00:12,190 --> 00:00:16,900 even with the same variable name contained within each one of them. 5 00:00:16,900 --> 00:00:20,649 That's because the variables are hidden from all other functions. 6 00:00:21,810 --> 00:00:25,560 Let's walk through the steps of creating a closure now. 7 00:00:25,560 --> 00:00:28,910 Open up the workspace with this video to follow along. 8 00:00:28,910 --> 00:00:31,040 Here's a simple function, doghouse. 9 00:00:33,040 --> 00:00:35,231 Like any function in JavaScript, 10 00:00:35,231 --> 00:00:39,923 doghouse can see variables declared outside of itself In the global scope 11 00:00:46,630 --> 00:00:50,750 As well as any variables declared inside in the local scope. 12 00:00:58,792 --> 00:01:04,247 However variables inside functions are not visible outside. 13 00:01:09,219 --> 00:01:15,100 If we log birds and dogs within the function we can read both variables. 14 00:01:15,100 --> 00:01:19,750 But logging them outside the function, we see that dogs isn't defined. 15 00:01:26,362 --> 00:01:31,180 Using the local scope that functions provide is what we can use to create 16 00:01:31,180 --> 00:01:31,990 closures. 17 00:01:33,200 --> 00:01:37,690 You may not know this, but you can declare a function inside another function. 18 00:01:38,820 --> 00:01:44,239 Let's use two functions together, declaring a function called showDogs. 19 00:01:47,830 --> 00:01:49,820 Within our dog house function. 20 00:01:52,110 --> 00:01:56,190 The showDogs function is called an inner function 21 00:01:56,190 --> 00:01:58,420 because it's inside another function. 22 00:01:59,560 --> 00:02:03,080 The dog house function is called an outer function. 23 00:02:07,030 --> 00:02:11,850 All variables within the outer function are visible to 24 00:02:11,850 --> 00:02:17,790 the inner function, even though they are hidden from the global scope. 25 00:02:17,790 --> 00:02:22,400 Just like the outer function has access to the global variables, 26 00:02:22,400 --> 00:02:28,290 the inner function has access to the outer function scope. 27 00:02:28,290 --> 00:02:33,020 Right now the dogs variable is declared in the outer function and 28 00:02:33,020 --> 00:02:34,880 the inner function has access to it. 29 00:02:36,320 --> 00:02:44,405 If the inner function is returned [BLANK-AUDIO] And 30 00:02:44,405 --> 00:02:50,770 is assigned to a variable [BLANK-AUDIO] 31 00:02:50,770 --> 00:02:58,166 The private scope of the outer function lives on and 32 00:02:58,166 --> 00:03:03,830 remains in the program's memory. 33 00:03:03,830 --> 00:03:08,670 In other words we can call the returned inner function and 34 00:03:08,670 --> 00:03:14,480 get access or even modify any private variables. 35 00:03:14,480 --> 00:03:18,580 A closure looks like this, an outer function, 36 00:03:20,860 --> 00:03:25,687 some private variables within the outer function scope, an inner 37 00:03:25,687 --> 00:03:30,840 function that could modify or log out, 38 00:03:30,840 --> 00:03:37,220 any of the private variables, and finally the inner function is returned. 39 00:03:37,220 --> 00:03:41,928 Every time the outer function is called a new private scope is created and 40 00:03:41,928 --> 00:03:44,760 the inner function returned can read and 41 00:03:44,760 --> 00:03:49,440 modify any private variables for this new private scope. 42 00:03:49,440 --> 00:03:52,750 This new scope is separate from any closures 43 00:03:52,750 --> 00:03:55,440 the outer function has created previously.