Closures allow JavaScript programmers to write better code. Creative, expressive, and concise. We frequently use closures in JavaScript, and, no matter your JavaScript experience, you will undoubtedly encounter them time and again. Sure, closures might appear complex and beyond your scope, but after you read this article, closures will be much more easily understood and thus more appealing for your everyday JavaScript programming tasks.





Can a Single JavaScript Course Make You Richer? This Single JavaScript Course Can Get You a Bigger Salary

(4 courses in 1—available as a single course for the first time) Becoming a JavaScript Expert Learn almost the entire JavaScript language, advanced JavaScript programming, software engineering for web programmers, and more 21 comprehensive major subjects and over 200 of the most important concepts covered (plus a bonus advanced course) Course covers from beginning to advanced to beyond advanced—including tooling, problem-solving, OOP, composition, software design patterns, software engineering foundation, architecting web applications, and much more Over 40 exercises and more than three dozen projects and applications

(Watching and/or reading JavaScript [JS] tutorials alone won't help you advance your programming career; you need to build real JS projects and applications as you learn. These exercises, projects, and applications are the most important part of your JS training. They will advance your JS confidence and skills and your overall programming and software engineering skills.) Every concept covered in depth and with clarity (if you don't understand something, we will update the course accordingly to add more clarity) Save yourself hundreds of hours and over $5,000 Plus: It comes with a HUGE BONUS, an entire advanced computer science JavaScript course: Discrete Math Concepts in JavaScript Become a JavaScript Pro and

significantly increase your income Learn More and Buy the Course By JavascriptIsSexy: Support the website while you simultaneously save yourself thousands of dollars



This is a relatively short (and sweet) post on the details of closures in JavaScript. You should be familiar with JavaScript variable scope before you read further, because to understand closures you must understand JavaScript’s variable scope.

What is a closure?

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.

You create a closure by adding a function inside another function.

A Basic Example of Closures in JavaScript:

function showName (firstName, lastName) { var nameIntro = "Your name is "; // this inner function has access to the outer function's variables, including the parameter function makeFullName () { return nameIntro + firstName + " " + lastName; } return makeFullName (); } showName ("Michael", "Jackson"); // Your name is Michael Jackson

Closures are used extensively in Node.js; they are workhorses in Node.js’ asynchronous, non-blocking architecture. Closures are also frequently used in jQuery and just about every piece of JavaScript code you read.

A Classic jQuery Example of Closures:

$(function() { var selections = []; $(".niners").click(function() { // this closure has access to the selections variable selections.push (this.prop("name")); // update the selections variable in the outer function's scope }); });

Closures’ Rules and Side Effects

