David Y.
—What is scope and how does it work in JavaScript?
The scope of a variable is the area of code in which it can be accessed. JavaScript has four different scopes:
{}
).We can illustrate what this means with a piece of example code:
let x = 1; // global scope console.log(x); // will print 1 function exampleFunction() { let x = 2; // function scope if (true) { let x = 3; // block scope console.log(x); // will print 3 } console.log(x); // will print 2 } exampleFunction();
This code will print the following to the browser console:
1 3 2
The three x
variables in the above example are defined at different scopes, and thus do not interfere with each other. One of the main advantages of scoping is that we can reuse the same variable names in different contexts without causing unexpected behavior.
Let’s consider a more complex example:
let globalVar = 1; // global scope console.log(globalVar); // will print 1 function exampleFunction() { // this is a block of code let functionVar = 2; if (true) { // this is another block of code console.log(globalVar) // will print 1 console.log(functionVar); // will print 2 let blockVar = 3; console.log(blockVar); // will print 3 } console.log(globalVar) // will print 1 console.log(functionVar); // will print 2 console.log(blockVar); // will throw an error } exampleFunction(); console.log(functionVar); console.log(blockVar);
This code will print the following to the browser console:
1 1 2 3 1 2 Uncaught ReferenceError: blockVar is not defined
If execution could reach them before failing, our attempts to access functionVar
and blockVar
in the global scope would throw similar errors. From this, we can see that variables from outer scopes can be accessed in inner scopes, but variables from inner scopes cannot be accessed in outer scopes.
Note that while variables defined with let
and const
adhere to block scopes, variables defined with var
do not. All three types of variables will still conform to the other three scope levels.
Finally, module scoping allows us to confine variables to individual JavaScript modules. We can illustrate this with the following HTML:
<!DOCTYPE html> <html lang="en"> <body> <script type="module"> let x = 1; console.log(x); // will print 1 </script> <script> console.log(x); // will throw an error </script> </body> </html>
As we’ve defined the variable x
inside a module, we cannot access it from another script tag. Without the use of modules, it would belong to the global scope, as below:
<!DOCTYPE html> <html lang="en"> <body> <script> let x = 1; console.log(x); // will print 1 </script> <script> console.log(x); // will print 1 </script> </body> </html>
You can read more about how to use JavaScript modules in this answer.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.