🌐
W3Schools
w3schools.com › js › js_scope.asp
JavaScript Scope
Variables declared Globally (outside any block or function) have Global Scope. Global variables can be accessed from anywhere in a JavaScript program.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-scope
Scope of Variables in JavaScript - GeeksforGeeks
3 days ago - Scope determines where a variable can be accessed or used within a JavaScript program. It helps control the visibility and lifetime of variables in different parts of the code.
Discussions

Javascript Variable Scope in 2.6 Minutes…
Any variable (var, let or const) declared outside of functions or any block of code (not within curly brackets { }) is global scope. This isn't the case since modules also define their own, non-global scope. Any declarations outside of functions or blocks in a module are part of that module scope, not global. More on reddit.com
🌐 r/learnjavascript
7
0
August 6, 2023
LET vs VAR, GLOBAL SCOPE VS LOCAL SCOPE
But can't let be considered global too? Yes. If declared in the global scope, let and const (and class) will also create global declarations. So the table is incorrect in that sense. Where this gets confusing is, unlike var and function, when let, const, and class are used in the global scope, they do not create global properties, whereas var and function do. var x = 1 let y = 2 console.log(globalThis.x) // 1 console.log(globalThis.y) // undefined This doesn't mean let, etc. are not defined in global, they just don't also exist as properties on the global object. This is possible because global scope has two buckets: an object record (global object and its properties) and a declarative record (declarations not in global object). Definitions in both contribute to those values which are globally available. So in the above example, var x is put into global's object record and let y is placed into the declarative record. What's nice about the declarative record is that it prevents collisions with properties of the global object. This can be especially important on the web as the global object is also the window object with many window-specific properties like name. If you've ever tried to create a name var in the global scope on the web, you may have noticed some problems. var foo = {} console.log(foo) // {} var name = {} console.log(name) // [object Object] What happens here is, var name ends up assigning to the pre-existing window.name property which will stringify values on assignment. The result is that the original object is lost and you're left with its string representation of "[object Object]". foo escapes this fate because it does not already exist on window. Variables defined in the declarative record do not have this problem. let name = {} console.log(name) // {} console.log(window.name) // '' or whatever the window name is In this case, our let name declaration is placed in the declarative record separate from the object record allowing it to coexist with the name property. The declarative record is checked first when referring to global variables by name so checking for just name gives us our declared value. For the object value, you can go directly through the global object accessing it as a property seen here with window.name. Where even if you declare the variable in block statement, it will automatically set it to globabal variable? This will only be the case if you use a non-block scoped declaration with a block statement in the global scope... and that can actually depend on strict mode too. Normal block-scoped declarations in a block do not become global. They're scoped to the block. let x // global { let y // block scoped, not global } Because var isn't block scoped, it will always create a global if in the global scope var x // global { var y // also global } If anywhere in your code - in global, in a block, in a function... - you assign a value to an undeclared variable, that will create a new global property. This isn't a declaration at this point, just an assignment, and if there is no variable of that name in scope, it defaults to assigning a global. function foo () { // function scope { // block scope x = 1 } } foo() console.log(x) // 1 This does not work in strict mode. Strict mode enforces assignments are to valid declarations (or global properties). So instead, an error is thrown. "use strict" function foo () { // function scope { // block scope x = 1 // Error! Does not exist } } foo() Strict mode also has an effect on function declarations making them block-scoped. Not var, only functions. So whereas normally a function in a block in the global scope would be global { function foo () { console.log('foo called') } } foo() // foo called In strict mode, the function would instead be scoped to that block and not be accessible as a global (or in the parent function scope if in a function) "use strict" { function foo () { console.log('foo called') } } foo() // Error! Does not exist So yeah, scope can be a little tricky in JS ;) More on reddit.com
🌐 r/learnjavascript
16
3
April 15, 2021
(Beginners) Understanding scope in JavaScript: Introduction
Thank you for this. A variable being auto-global without var was particularly helpful. I just finished the JS course on code academy and I had a question about this which wasn't fully answered. More on reddit.com
🌐 r/learnjavascript
5
33
March 2, 2017
How am I declaring a "const" twice in a switch statement?
Because they're both in the same scope. It doesn't matter if logically they can never be created at the same time. The error is a SyntaxError which happens before the code even runs. Generally, to fix this, you can give each case their own block scope case 3: { const bla="MEEEEPT;" break; } case 5: { const bla="mOOOOOO"; break; } But in your case you're trying to access bla outside of the switch/cases. So in that case you'd want to declare it outside of the switch like a and reassign (not declare) inside the cases. let a=5; let bla; switch (a) { // ... And at this point the block scopes for the cases wouldn't be needed. More on reddit.com
🌐 r/learnjavascript
14
3
August 14, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Scope
Scope - Glossary | MDN
However, the following code is valid due to the variable being declared outside the function, making it global: ... const x = "declared outside function"; exampleFunction(); function exampleFunction() { console.log("Inside function"); console.log(x); } console.log("Outside function"); console.log(x); Blocks only scope let and const declarations, but not var declarations.
🌐
Programiz
programiz.com › javascript › variable-scope
JavaScript Variable Scope (with Examples)
In JavaScript, a variable declared outside any function or in the global scope is known as a global variable.
🌐
freeCodeCamp
freecodecamp.org › news › scope-in-javascript-global-vs-local-vs-block-scope
Scope in JavaScript – Global vs Local vs Block Scope Explained
September 4, 2024 - In JavaScript, global scope is the widest scope available. Variables declared in global scope are accessible from anywhere in your code, whether it's inside functions, conditional statements, loops, or other blocks of code.
🌐
SitePoint
sitepoint.com › blog › javascript › demystifying javascript variable scope and hoisting
Demystifying JavaScript Variable Scope and Hoisting — SitePoint
November 13, 2024 - Understanding and correctly utilizing ... scope of a variable is controlled by the location of the variable declaration, and it defines the part of the program where a particular variable is accessible....
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-variables-scope-hoisting-in-javascript
Understanding Variables, Scope, and Hoisting in JavaScript | DigitalOcean
August 24, 2021 - Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript.
Find elsewhere
🌐
DEV Community
dev.to › yugjadvani › five-types-of-scope-in-javascript-a-deep-dive-for-developers-285a
Six Types of Scope in JavaScript: A Deep Dive for Developers - DEV Community
September 16, 2024 - JavaScript’s behavior with variables is governed by its scope. Understanding scope is fundamental for writing robust, maintainable code. This article will explore the five main types of scope in JavaScript — Global, Local, Block, Function Scope (and Closures), Scope Chain and Module Scope.
🌐
web.dev
web.dev › articles › global and local variable scope
Global and local variable scope | Articles | web.dev
April 14, 2022 - Variables with global scope are available from all other scopes within the JavaScript code. Variables with local scope are available only within a specific local context and are created by keywords, such as var, let, and const. If you use the var, let or const keywords to create a variable ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › var
var - JavaScript | MDN
Initial value of the variable. It can be any legal expression. Default value is undefined. The scope of a variable declared with var is one of the following curly-brace-enclosed syntaxes that most closely contains the var statement:
🌐
Reddit
reddit.com › r/learnjavascript › javascript variable scope in 2.6 minutes…
r/learnjavascript on Reddit: Javascript Variable Scope in 2.6 Minutes…
August 6, 2023 -

Here is a post about scoping and javascript variables. Differences in scope mean differences in accessibility and usability for the variables that we declare. I hope this post helps everyone learn or brush up on their knowledge when it comes to declaring variables. Please feel free to reach out with any questions, queries, comments or banter!

https://thecodingapprentice.substack.com/p/javascript-variable-scope-in-26-minutes?sd=pf

🌐
Simplilearn
simplilearn.com › home › resources › software development › javascript tutorial: learn javascript from scratch › what is the scope of variables in javascript and its types
What Is the Scope of Variables in Javascript | Simplilearn
November 26, 2024 - Explore the scope of variabes in Javascript and its types in detail. Read on to know how the global, local, block and function scope types help in accessibility of variable.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Study.com
study.com › computer science courses › introduction to javascript
Variable Scope in JavaScript: Explanation & Examples | Study.com
In the example above, the variable mammalName is local to the function animalName(). Only the code inside the function animalName() can access mammalName. The variable loses its scope outside of the function. Thus, the variable has a function or local scope. Local variables are always declared with the keyword ''var''. A local variable gets created when the JavaScript function begins and is deleted at the end of the function.
🌐
DEV Community
dev.to › micmath › javascript-variable-scope-by-example-3970
JavaScript Variable Scope by Example - DEV Community
May 30, 2025 - Inner functions can access variables from their outer (enclosing) functions, creating a nested "scope chain." JavaScript provides three keywords for declaring variables: var, let, and const.
🌐
O'Reilly
oreilly.com › library › view › javascript-the-definitive › 0596000480 › ch04s03.html
Variable Scope - JavaScript: The Definitive Guide, Fourth Edition [Book]
November 19, 2001 - Variable Scope The scope of a variable is the region of your program in which it is defined. A global variable has global scope -- it is defined everywhere in your JavaScript code....
Author   David Flanagan
Published   2001
Pages   936
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Grammar_and_types
Grammar and types - JavaScript | MDN
(See Variable scope below.) You can declare variables to unpack values using the destructuring syntax. For example, const { bar } = foo. This will create a variable named bar and assign to it the value corresponding to the key of the same name from our object foo. Variables should always be declared before they are used. JavaScript used to allow assigning to undeclared variables, which creates an undeclared global variable. This is an error in strict mode and should be avoided altogether.
🌐
daily.dev
daily.dev › home › blog › get into tech › concepts in javascript: understanding scope
Concepts in JavaScript: Understanding Scope
February 4, 2025 - Scope Types: JavaScript has three main types of scope: Global, Function, and Block scope. Keywords: Variables are declared using var, let, or const, each affecting scope differently.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Variable scope, closure
As you may have read in the article Variable scope, closure, a variable starts in the “uninitialized” state from the moment when the execution enters a code block (or a function).
🌐
Udacity
udacity.com › blog › 2021 › 06 › javascript-global-variable-scopes.html
Controlling Execution Contexts with Javascript Global Scope and Javascript Variable Scopes | Udacity
June 16, 2021 - The Javascript global scope is the context where everything in a Javascript program executes by default. This scope includes all variables, objects, and references that are not contained within a customized scope defined by a programmer.
🌐
Reddit
reddit.com › r/learnjavascript › let vs var, global scope vs local scope
r/learnjavascript on Reddit: LET vs VAR, GLOBAL SCOPE VS LOCAL SCOPE
April 15, 2021 -

Hello, my background is java and I am trying to understand variable declaration deeper. Mainly what the differnces between let and var keywords. I found this great website for those who are still looking to understand.

https://scotch.io/tutorials/understanding-scope-in-javascript#toc-block-statements

But I am still having trouble how LET is not a global scope? If you google let vs var, you will get tons of tables like this:

But can't let be considered global too? Since I can declare it globally. Or is it referring to automatic globlazation? Where even if you declare the variable in block statement, it will automatically set it to globabal variable?

Sheesh, I have under estimated the JS. Thanks.

Keywords for people looking for this:

LET vs VAR,

GLOBAL SCOPE VS LOCAL SCOPE

Global scope vs block statement

scope vs block

Top answer
1 of 5
6
But can't let be considered global too? Yes. If declared in the global scope, let and const (and class) will also create global declarations. So the table is incorrect in that sense. Where this gets confusing is, unlike var and function, when let, const, and class are used in the global scope, they do not create global properties, whereas var and function do. var x = 1 let y = 2 console.log(globalThis.x) // 1 console.log(globalThis.y) // undefined This doesn't mean let, etc. are not defined in global, they just don't also exist as properties on the global object. This is possible because global scope has two buckets: an object record (global object and its properties) and a declarative record (declarations not in global object). Definitions in both contribute to those values which are globally available. So in the above example, var x is put into global's object record and let y is placed into the declarative record. What's nice about the declarative record is that it prevents collisions with properties of the global object. This can be especially important on the web as the global object is also the window object with many window-specific properties like name. If you've ever tried to create a name var in the global scope on the web, you may have noticed some problems. var foo = {} console.log(foo) // {} var name = {} console.log(name) // [object Object] What happens here is, var name ends up assigning to the pre-existing window.name property which will stringify values on assignment. The result is that the original object is lost and you're left with its string representation of "[object Object]". foo escapes this fate because it does not already exist on window. Variables defined in the declarative record do not have this problem. let name = {} console.log(name) // {} console.log(window.name) // '' or whatever the window name is In this case, our let name declaration is placed in the declarative record separate from the object record allowing it to coexist with the name property. The declarative record is checked first when referring to global variables by name so checking for just name gives us our declared value. For the object value, you can go directly through the global object accessing it as a property seen here with window.name. Where even if you declare the variable in block statement, it will automatically set it to globabal variable? This will only be the case if you use a non-block scoped declaration with a block statement in the global scope... and that can actually depend on strict mode too. Normal block-scoped declarations in a block do not become global. They're scoped to the block. let x // global { let y // block scoped, not global } Because var isn't block scoped, it will always create a global if in the global scope var x // global { var y // also global } If anywhere in your code - in global, in a block, in a function... - you assign a value to an undeclared variable, that will create a new global property. This isn't a declaration at this point, just an assignment, and if there is no variable of that name in scope, it defaults to assigning a global. function foo () { // function scope { // block scope x = 1 } } foo() console.log(x) // 1 This does not work in strict mode. Strict mode enforces assignments are to valid declarations (or global properties). So instead, an error is thrown. "use strict" function foo () { // function scope { // block scope x = 1 // Error! Does not exist } } foo() Strict mode also has an effect on function declarations making them block-scoped. Not var, only functions. So whereas normally a function in a block in the global scope would be global { function foo () { console.log('foo called') } } foo() // foo called In strict mode, the function would instead be scoped to that block and not be accessible as a global (or in the parent function scope if in a function) "use strict" { function foo () { console.log('foo called') } } foo() // Error! Does not exist So yeah, scope can be a little tricky in JS ;)
2 of 5
2
I don't understand why all these resources introduce scope by talking about 'global scope', 'block scope', 'local scope', 'function scope' etc as opposed to how Javascript actually implements scope: environment records. I just feel like this is one of the few cases where reading the ECMASCript spec is actually clearer than much of the third party material out there. As a bonus you get to learn exactly how closures work!