JavaScript Scope and Closures

JavaScript Scope and Closures

Scopes and closures are important in JavaScript. But, they were confusing for me when I first started. Here's an explanation of scopes and closures to help you understand what they are.

[…]

A scope in JavaScript defines what variables you have access to. There are two kinds of scope – global scope and local scope.

[…]

This is true only with JavaScript in web browsers. You declare global variables in Node.js differently, but we won't go into Node.js in this article.

[…]

In JavaScript, there are two kinds of local scope: function scope and block scope.

[…]

The first way is to add the debugger keyword in your code. This causes JavaScript execution in browsers to pause so you can debug.

[…]

If you liked this article, you may also like other front-end-related articles I write on my blog and my newsletter. I also have a brand new (and free!) email course: JavaScript Roadmap.