From the course: Learning Functional Programming with JavaScript ES6+

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Understanding closure

Understanding closure

- [Instructor] All right, so previously we talked about how it's possible to return functions from other functions in JavaScript. And this concept actually leads us to another very important concept in functional programming, and this is something called closure. Now, basically, closure means that when we return a function from another function as we've done here, oops, let me try that again. There we go. The function that we return still has access to the scope that it was returned from. So that probably doesn't mean a whole lot to you yet. So let me show you an example that will demonstrate closure. First of all, I'm going to create a new file here, which we'll call something like closure.js And inside here what we're going to do is we're going to create a function that returns a function. We'll call this createPrinter again. But the difference here is that this function is going to have a variable inside of it called something like my favorite number, and we'll make that 42. All…

Contents