Sitemap
Elaine Doud

Elaine Doud

Scope

3 min readOct 30, 2022

As a new software developer, there have been several times when my code in JavaScript does not work and at first I am very puzzled as to why. In these instances it ended up being an issue of scope. As explained by W3Schools, functional scope means that if variables are declared within a function, they cannot be called or referenced outside that function. In contrast a global variable can be used anywhere else in your JavaScript environment. Attempting to call a local variable (ie. variable inside another function) may result in an error message or the code simply not working.

In theory this is straight forward, but when writing more complicated code it can become frustrating that a variable you declared earlier may not work as intended. As shown in the code below, the variable theGames was declared inside the function showGames. When I attempted to use the variable theGames when creating a new function it did not work. This is because it was already nestled inside another function. Once I moved the variable outside of the function by placing it at the top of my page, it became global, and I was able to use it in the rest of my code.

Press enter or click to view image in full size

Functions and objects also follow these rules regarding scope. A function nested within another function cannot be called elsewhere. The same goes for objects. If you create an object and are anticipating that you may need it again, declare it as a global variable rather than nesting it within a function. This helps us reach one of the goals of coding, which is to avoid repeating code when you can. It is an important, delicate balance to make sure your code is organized, but that you are also able to access what you need as you continue to add onto it.

Get Elaine Doud’s stories in your inbox

Join Medium for free to get updates from this writer.

It is also important to be aware that variable names nested within functions can be used elsewhere for different values. You can make use of this functionality if you choose, although this will likely make it more difficult to keep track of your variables. It will increase the chance of an error being made in the code. This is helpful to be aware of, because if you have a lot of code and are encountering errors, it is possible you may have inadvertently given two variables the same variable name.

Global scope is when your variables can be accessed anywhere in your code. Var, const, and let can all be used for global variables, but using either const or let is considered best practice. Local scope is when a variable can only be accessed within the function it is called in. Remember, objects and functions can also be variables. Block scope is when a variable can only be called from the block of code in which it was declared. A block of code in this case means between curly brackets { }.

Sources: JavaScript Scope. W3Schools. https://www.w3schools.com/js/js_scope.asp

JavaScript Variable Scope. Programiz. https://www.programiz.com/javascript/variable-scope

--

--