Hey! In the previous challenges, you were introduced to variables and in essence they allow you to store different types of values. You can think of them as same variables youโ€™ve used in grade 8th or 9th level mathematics class. To understand this concept lets use an example. You are creating a โ€ฆ Answer from staranbeer on forum.freecodecamp.org
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Global_scope
Global scope - Glossary | MDN
In a programming environment, the global scope is the scope that contains, and is visible in, all other scopes. In client-side JavaScript, the global scope is generally the web page inside which all the code is being executed.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Global Scope and Functions - JavaScript - The freeCodeCamp Forum
Hi there, I am currently doing JS Data Structures and Algorithms โ†’ Global Scope and Functions It says: โ€œIn JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope.
Published ย  July 17, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-scope
Scope of Variables in JavaScript - GeeksforGeeks
June 6, 2023 - A var variable declared inside a function is accessible throughout that entire function, regardless of any blocks (like if statements or for loops) within the function. If var is declared used outside of any function, it creates a global variable.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Scope
Scope - Glossary | MDN
If a variable or expression is ... scopes, but not vice versa. JavaScript has the following kinds of scopes: Global scope: The default scope for all code running in script mode....
๐ŸŒ
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 - 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. You can think of global scope as the "public square" of your program, where everyone can see and access what's going on. To illustrate, let's consider an analogy. Imagine your JavaScript program as a small town, and the global scope as the town's central square.
๐ŸŒ
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 ...
๐ŸŒ
Contentful
contentful.com โ€บ blog โ€บ the-global-object-in-javascript
What is the global object in JavaScript? A practical guide for developers | Contentful
March 14, 2024 - ... scopes (also called contexts) determine where a variable or function that you have declared is accessible. Each scope has access to the functions and variables declared in its parent scopes.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 - Global scope is the entire Javascript execution environment. Any variables assigned without an explicit scope (using the var, let, or const keywords) automatically become global variables.
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Glossary โ€บ Global_object
Global object - Glossary | MDN
The globalThis global property allows one to access the global object regardless of the current environment. var statements and function declarations at the top level of a script create properties of the global object. On the other hand, let and const declarations never create properties of ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ scope in javascript
Scope in JavaScript | Sentry
The scope of a variable is the area of code in which it can be accessed. JavaScript has four different scopes: Global scope: available across all code.
๐ŸŒ
Wes Bos
wesbos.com โ€บ javascript โ€บ 03-the-tricky-bits โ€บ scope
Scope - Wes Bos
Methods that are globally available to us like setTimeout() are actually on the window object, and that is what is called global. There is one thing to know about const and let. Let's say we added this code to scope.js. /* eslint-disable */ const first = 'wes'; let second = 'bos'; var age = 100; If you reload scope.html in the browser and then in the console type in first, second and age, you will see the following values returned because they are all global variables ๐Ÿ‘‡
๐ŸŒ
Medium
olaishola.medium.com โ€บ understanding-javascript-scope-global-vs-function-scope-8f19cf107b25
Understanding JavaScript scope: Global vs. function scope | by Oladipupo Ishola | Medium
March 11, 2023 - Variables declared outside of any function are regarded as having global scope. This means they can be seen and accessed from anywhere in your code, including any functions you define.
๐ŸŒ
Playcode
playcode.io โ€บ javascript โ€บ variable-scope
JavaScript Variable Scope: Global, Local & Block Scope Explained | PlayCode
JavaScript has three main types of scope: ... Proper use of scope helps you write cleaner code, prevent naming conflicts, manage memory efficiently, and avoid hard-to-find bugs. Variables declared outside any function or block have global scope.
๐ŸŒ
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!
๐ŸŒ
Dmitri Pavlutin
dmitripavlutin.com โ€บ javascript-scope
A Simple Explanation of Scope in JavaScript
The global scope is a mechanism that lets the host of JavaScript (browser, Node) supply applications with host-specific functions as global variables. window and document, for example, are global variables supplied by the browser.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ what-is-variable-scope-in-javascript
What is Variable Scope in JavaScript ? | GeeksforGeeks
In JavaScript, scope defines the accessibility of variables in different parts of your code. It determines where a variable can be accessed or modified.Before ES6 (Released in 2015), variables were declared only with var, which is function-scoped (accessible within the function) and global Scoped (A
Published ย  March 5, 2024
๐ŸŒ
DEV Community
dev.to โ€บ ale3oula โ€บ the-hor-r-o-r-scope-global-local-and-block-scope-in-js-37a1
The horror-scope - Global, Local and Block scope in JS - DEV Community
March 19, 2025 - In this article, we will explore the different types of scopes in JavaScript: global, local, and block scopes.