DEV Community

miguelf1671
miguelf1671

Posted on

JavaScript Scope

JavaScript is still used widely in programming for better or for worse and scope in javaScript is super important. Imagine scope as a way of organizing your room and keeping track of everything you store away. If you put things in all different cabinets and shelves how will you ever find things and make good use of them? It will probably be very hard for you to keep track of everything that way, and it can be very frustrating.

So what is scope in javaScript? Scope sets boundaries of where and when you can use variables and functions you have created and it's functionality will depend on that.

There are two main types of scope:

Global scope: When you have a global scope is like placing something in your living room. Anyone in the room can see it and touch it because you made it accessible to everyone. In code this would translate to giving multiple functions that global variable to use and manipulate because its available to use anywhere in your code.

Local scope: When you create a local scope you can think of it as every room in your house is a function, and in those rooms you add a specific item that is unique to that room. Other rooms would not have access to those unique item's, they can't see them or touch them, therefor they are local to that specific room. In code its the same, when you place a variable inside a function, only that function will have access to that variable.

In conclusion scope is very important in JavaScrript, it will determine how you organize your code and ultimately how your code runs or doesn't run.

Top comments (0)