Scope is one of the most important topics in JavaScript. In this tutorial, you will learn about what scope is. Then, you will learn about types of scope and how they work. You will also learn about ES6 variables, block statements, why they matter for scope. Lastly, you will also learn about closures.

The basics of scope

So, what is scope? In programming, scope refers to the visibility and accessibility of variables, functions and objects during runtime. Put simply, scope says whether you can use particular variable, functions or object in your code at a specific location or not. And the runtime? Runtime is a time during which a computer program is executing.

This is important. It means that not all variables, functions and objects are always visible and accessible everywhere. Thanks to scope, variables, functions and objects may be visible and accessible everywhere, or they may not. It depends in what scope do you create them. If they are not visible, JavaScript will not let you use them.

What if you try to use some variable, function or object that is not accessible in your current scope? JavaScript will tell you that the variable, function or object is not defined. This is correct because that thing is really not defined. It doesn’t exist in your current scope.

One more thing about scope. You can also create scope inside a scope, or let’s say “child” scopes. These are called lexical scopes. In this case, these lexical scopes can access variables, functions or objects defined in a parent scope. However, parent scope can’t access variables, functions or objects defined in a its lexical scopes.

Don’t worry if this sounds too difficult now. We will talk more about all this, and more, later in this tutorial. There will also be examples that will help you understand all these concepts. But before that, let’s first talk about some benefits of scope.

#javascript #programming #design development

JavaScript Scope Explained
1.10 GEEK