JavaScript Closures

JavaScript variables can belong to the local or global scope. Global variables can be made local (private) with closures. Global Variables A function can access all variables defined inside the function, like this: Example function myFunction() { var a = 4; return a * a; } Try it Yourself » But a function can also access variables defined outside the function, like this: Example var a = 4; function myFunction() { […]