DEV Community

Shawn Smith
Shawn Smith

Posted on

Scopes

The scope is the current content of execution that the values and expressions are visible and can be referenced. When a variable is not in the current scope it won't be able to be used.

There are different kinds of scopes; There is the Global, Local, Function and Block scopes.

Global scope is the default scope where we would have access to the variables anywhere in our code.

Local scope is the a characteristic of variables that make them local. This is a variable that it bound to its value within a scope that is not Global.

Function scope is a scope created with a function.

Block scope is a scope created with a pair of curly braces making a "block".(Block scopes only scope let and const declarations but not var.)

Image description

Knowing the different types of scopes can be a helpful tool in debugging. We can be searching our code for why something isn't running and it can be something as simple as having a variable declared in a function scope and then trying to access it globally. It's important to note when starting to code it may be easier to declare certain variables globally when it is known you will use them multiple times in different scopes.

Globally declared variables you can use as many times as you need all over your code.

For more detailed information go to MDN:
Scope: https://developer.mozilla.org/en-US/docs/Glossary/Scope

Top comments (0)