Global Socope - Error in the lesson?

Tell us what’s happening:
Hi, in this lesson im been told that every varible which is not declared with “let” or “const” will have a Global Scope. With that statement I can think that, then, every variable declared with “let” or “const” will not be on a Global Scope. But, in the exersice of the lesson I am been asked to create a GLOBAL variable WITH let ot const, and for what I was told before that is impossible, at least, in my understanding…

Thank you so much, I’d love to know if I am wrong or if the lesson is, I want to get ir right.
Thanks again!

  **Your code so far**

// Declare the myGlobal variable below this line

var myGlobal = 10;
function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;

// Only change code above this line

function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
  output += "myGlobal: " + myGlobal;
if (typeof oopsGlobal != "undefined") {
  output += " oopsGlobal: " + oopsGlobal;
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Global Scope and Functions

Link to the challenge:

Global means it’s available everywhere. In this tiny program the variable is available everywhere that matters to the program.

You can’t accidentally create variables that leak outside of that with let/const, that is what is meant by not being able to create global variables. var variables that are not declared inside a function are attached to the global object (Window in browsers). let/const variables are not.

But regardless of that, myGlobal is available to and can be modified by everything else in this program, it is global to that program

1 Like

Hi Dan, thanks for replying.

What you mean is that every variable that I declare with let or const outside of a function won’t be have a Global Scope? If it is that way, then, why I was asked to create a Global Variable exclusively with let or const, is it not impossible?

I did neither undeerstand your statement that I can’t create a variable that leak outside of having Global Scope with let or const, because of if the variables that I declare outside of a function with let/const won’t have a global scope, then, they will indeed leak outside…

Sorry, I am trying hard to get it…
Thanks again

Yes sorry reading that back it was a bit confusing, sorry. JS code executes in a scope, in some context (is has to, where would you access built in functions from otherwise?). In the browser that context is almost always an object called Window. var variables declared outside a function will attach to that. let variables will not.

var x = 'global';
let y = 'global';
console.log(globalThis.x); // "global"
console.log(globalThis.y); // undefined

Practically, right now, this isn’t going to make a huge difference to how you code, it’s just how they work.

If you have a program that’s all in, say, one file, and at the top of the file are some variables declared with let, then you do loads of other stuff in the program, those variables are still global to the program, everything in it can access them.

If you have lots of JS files, and you load them all in script tags in a browser and run the code, then the variables won’t be global. Any variables declared in the top level scope won’t be shared. If you’d used var they would be.

1 Like

It might also be worth making a distinction between global and top-level variables. A global variable (to me) is something on the global object and a top-level variable is just at the highest level of the most outer execution context.

As said. Variables declared using var are function scoped so if you declare them outside functions they will end up on the global object (window object in the browser). Variables declared using let are block-scoped and will not end up on the global object even when declared outside of any scope (block or function).

var global = "var test";
console.log(; // var test

let notGlobal = "let test";
console.log(window.notGlobal); // undefined

function canReadOuterScope() {
  console.log(global); // var test
  console.log(notGlobal); // let test

1 Like

So in this particular case (with the oopsGlobal, javascript looks in the current block (the nearest {}) for a let or const defined with that name. Not finding it, it might keep going out blocks (“blocks” being delimited with curly braces), until it’s looking at the functions scope to see if there might be a var, let or const here.

Failing to find one, it would continue out (say our function was inside an object or something), checking broader and broader scope, until it reaches the global window scope.

Still not having found a variable named oopsGlobal it creates one for us. And as we haven’t defined what or where, it creates it in the outermost scope. It can’t know our intent (and early javascript devs were lazy), so it makes that variable as broadly available as possible.

1 Like

Thank you all so much. As each exlained it different to me I think I have a broder and better uderstanding.
Great community hear!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.