Member-only story

Scoping in Javascript

Where and when variables will be accessible in your code.

Alex Z
4 min readOct 4, 2020

Scoping is important because we want to as much as possible follow the Principle of Least Privilege when writing programs — that is, if you don’t need to reveal your variable to the rest of your program, then keep its scope isolated. This important for many reasons, including reducing the chance of creating bugs/side effects, and increasing the efficiency of garbage collecting of variables while your program is running.

There are many articles out there on this topic so I wanted to really quickly break it down in a way that makes sense to me in 2020 (because things do change as technology and browser support change in the JS community).

One Scope To Rule Them All

There is really one type of scope if you are writing JS in ES6, and that is the block scope, or local scope. Function scope behaves the exact same as block scope, as long as you are using the ES6let keyword to declare your variables.

The reason that so many articles are written on this topic is that before the introduction of let and const keywords in ES6, variables declared with the var keyword would actually operate differently if declared or initialized in a function or in a block. A great example of this discrepancy is below : did you know that you can actually access ‘i’ from outside your for loop? This is because of the weird behavior of var. For the rest of the article I will use the keyword…

--

--

Alex Z
Alex Z

Written by Alex Z

Software Developer + Product Manager. Interested in Travel, Culture, Economics and the Internet. Join Medium: https://tinycode.medium.com/membership

No responses yet