From the course: JavaScript: Scope

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

What are closures?

What are closures? - JavaScript Tutorial

From the course: JavaScript: Scope

Start my 1-month free trial

What are closures?

- [Instructor] Closures are an inner function that has access to external variables. By creating a function inside of a function, we create a closure. So let's do some code to understand and visualize this. So we'll leverage some of the code that we did on the last video, and the first thing we'll do is simply remove the console.log here, and then do a function inside of that function. We'll return an object, and inside of that object we'll have a function called shootWarrior, which will return a console.log. And this console.log will have warrior, and then warrior2. So if I comment the console.log on line 15, and I save this, and I go back to Chrome here, I shouldn't have anything here, so what we need to do is create another variable from the function here, which is const, and let's call this one newWarrior, and then pass the function screamWarrior, and then use newWarrior, and then run that function. Because that…

Contents