Closures in Javascript explained

Closures are important because they control what is and isn’t in scope in a particular function. A closure gives you access to an outer function’s scope from an inner function. Closures are frequently used in JavaScript for object data privacy, in event handlers and callback functions.

See the following example:

function init() {
    var text = "Hello World!"; // text is a local variable created by init
    function sayHello() { // sayHello() is the inner function, a closure
        console.log(text); // sayHello() uses variable text declared in the parent function    

Nested functions have access to variables declared in their outer scope.

Every time curly brackets are used a lexical environment is created, this is purely a logical concept. The let keyword conseils the variable to its own scope (scope is the same as lexical environment), so this is useful if you want to make a private variable.


You are seeing this because your Disqus shortname is not properly set. To configure Disqus, you should edit your _config.yml to include either a disqus.shortname variable.

If you do not wish to use Disqus, override the comments.html partial for this theme.