This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS, v2 course:
The "Closure, Part 2" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Bianca adds a setTImeout() method to the function, demonstrating that even a delay, the nested function still has access to the parent function�۪s scope.

Get Unlimited Access Now

Transcript from the "Closure, Part 2" Lesson

>> Next one. It's a little bit different. Who wants to start this time? Jamie.
>> So, you set up my alerts, and it's runs a function.
>> Has not yet run anything but-
>> Yeah, but I assume it will next.
>> So myAlert we had to call every time and then, yeah.

[00:00:20] So help, I found a clue. And then what happens, Michael?
>> Then you define another function called alerter, which actually sets an alert with x as the argument and-
>> Doesn't do that yet. It sets this but it doesn't actually run the body of this function.
>> Yeah, that's right, okay.

>> Yeah, okay?
>> Yeah.
>> And then what happens next, Aisha?
>> Alert gets x passed into it.
>> What happens next? So we define this and then what happens? Time the alerter?
>> Yeah, so we call the set timeout. And what happens with a set timeout is alerter goes away into somewhere deep in browser land.

[00:01:19] And it's gonna get called in a thousand seconds. And the question is, what happens first? The log or the alert?
>> The log.
>> Why?
>> Because alerter won't be run for another thousand seconds.
>> Yeah, because it's asynchronous. It will go away. The alerter will call but it doesn't block the execution of our function.

[00:01:46] But alerter will still be called later and sometime in the future, right? We're used to that? Nothing surprising?
>> To set Timeout, automatically call a function without the parenthesis. Without invoking it, like it was just an automatic-
>> If you had put parentheses here-
>> Yeah.
>> It would try to call whatever this returns.

[00:02:13] So it would call it first and then try to call it again and it would mess up.
>> Okay, so it just takes a function and then-
>> Yeah, and it calls it later, exactly. Cuz you only wanna put the parentheses at the moment you wanted to execute the body of the function.

[00:02:31] And so since this would return undefined, we'd really be trying to call undefined somewhere. And we've all seen those values. Undefined is not a function.