 Unlike in Pigeon, a function in JavaScript can be created inside another function. A function created inside another is often called a nested function. There are five things to understand about nested functions. First, a function statement inside another function assigns the newly created function not to a global variable, but instead to a local variable of the enclosing function. So here inside the function foo, a new function is created and assigned to the local variable bar of the function foo. Second, variables declared in a nested function, including the parameters, are local to that function, not the enclosing function. Here, for clarity, I've highlighted the variables of the outer function foo in orange and highlighted the variables of the inner function bar in red. Again, note that the name bar itself is confusingly a local variable of the outer function foo. Third, declare that nested functions are created in some particular call to the outer function. Each call to foo here creates another copy of the function bar. Fourth, a nested function can use the variables of the function call in which it was created. Here, each individual copy of the function bar can use the local variables of the particular call to foo in which that copy of bar was created. Now, function nesting may go deeper than one level. For example, say in some call to a function we'll call orange, say we create another function we'll call red, and say in a call to that function we create another function we'll call green. And let's say that the orange function has three variables named a, b and c, the red function has two variables named a and d, and the green function has two variables b and e. Well, in this scenario, each copy of the red function can use the variables of the call to orange in which that copy of red was created, and each copy of the green function can use those variables as well, but also the variables of the call to the red function in which that copy was created. The caveat is that some of these variable names conflict, both the red and orange function, for example, have a variable named a. The way this gets resolved is that the more local variables take precedence, so the red function only sees b and c of the orange function, but not its a, and the green function sees a and d of the red function, but only c of the orange function. So if a name conflict prevents you from accessing a variable of an enclosing function, you can always just resolve the name conflict. For example, here if we want to use b of the orange function in our green function, we could just rename b of the green function to some other name that doesn't conflict. Be clear that a nested function can modify an enclosing function's variables. Here in the call to foo, the nested function bar increments the variable a of foo, and so the call to foo returns four, not three. The fifth and last thing to understand about nested functions is that through a language feature called closure, a nested function can use the variables of the function call in which it was created even after that function call returns. Normally, the local variables of a function call disappear once the call returns, but closure preserves the variables for use by the nested function. Now in our example here, the nested function bar is only stored in a local variable of foo, so the function bar disappears along with the local variables of foo when the call to foo returns. If however we return bar itself from the call to foo, or if we store it in some variable array or object outside the function foo, then the function bar will outlive the call to foo and so might make use of closure. Here, the nested function bar returns a sum of its parameter b and the parameter a of its enclosing function foo, and bar itself is returned from foo. So when we call foo with an argument two, it returns a copy of the function bar for which the retained variable a of foo has the value two. Likewise, when we call foo with an argument three, it returns another copy of the function bar for which the retained variable a of foo has the value three. So be clear, we have two separate copies of the function bar, one assigned to Greg and one to Lisa, and these copies differ in their closures. Each closure has its own set of the variables of the enclosing functions. The variable a in one closure is not the same variable a as in the other. So when we then invoke these two separate copies of bar with the argument six, the copy stored in Greg returns eight, because six added to two returns eight, but the copy stored on Lisa returns nine, because six added to three returns nine. For another example, here the nested bar function has no parameter of its own, but increments a of the enclosing foo function. We assign to Lisa the copy of bar returned by foo with an argument of five, and then each time we invoke Lisa, it increments the variable a of its closure by two each time. So we get the values seven, the nine, then 11, and so forth.