Scopes in JavaScript

Scope is an important, yet debatable concept in JavaScript. In this article, we will study the different types of scope in JavaScript and how they work in order to write better code.
What is Scope?
In JavaScript, Scope means variable access which means when a piece of code is running what all are the variables you have access to. Scope determines the accessibility of these variables.
Before ES6 JavaScript we had only functional scopes i.e Global and Local scope. But with ES6 now we have Block level scope too.
What is Global Scope?
Before you write a line of JavaScript, you’re in what we call the Global Scope.

Anything you declare outside the curly brackets in the JavaScript program is Global It means you can access the variable anywhere in the program.

You’ll often hear people saying “Global Scope is bad”, but never really justifying to why. Global scope isn’t bad, you need it to create Modules/APIs that are accessible across scopes, you must use it according to your advantage.
What is Local Scope?
Variables defined inside a function are in the local scope. And they have a different scope for every call of that function. This means that variables having the same name can be used in different functions.

name is scoped locally, it isn’t exposed to the parent scope and therefore undefined.What is Block Scope?
Until today, JavaScript comes with a function-level scope for variables and functions. With ECMAScript 6, the situation will change with the availability of the well-understood block scope. ECMAScript 6 introduced the let and const keywords. These keywords can be used in place of the var keyword. Block statements like if and switch conditions or for and while loops, unlike functions, don't create a new scope. Variables defined inside of a block statement will remain in the scope they were already in.

That’s it. Feel free to reach out to me anytime if you want to discuss something :)
Github: https://github.com/The-lady-developer
Twitter : https://twitter.com/manvi_singhwal
