Basic JavaScript - Global Scope and Functions

I found this challenge to be more confusing than it probably should have been.

Specifically regarding the following statement: " Variables which are declared without the let or const keywords are automatically created in the global scope."…

Since this statement did not also mention var, this led me to believe that variables declared using var automatically become Globally scoped, even when located within a function.

After doing some googling, reading the hints and watching the video, I learned that variables can be declared without using any of the var, let or const keywords. Which I did not know, or at least dont remember being covered til now.

So now I know that variables can be declared without using those 3 keywords and that is the scenario in which a variable becomes automatically Globally scoped.

Maybe the challenge was designed that way on purpose to force the habit of additional research. I just feel like this challenge would be a lot less confusing if var had also been mentioned in the above statement I referenced.

// Declare the myGlobal variable below this line
let 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;
  }
  console.log(output);
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Basic JavaScript - Global Scope and Functions

Link to the challenge:

Var exists but its a legacy feature. You’d be better off forgetting about its existence 95% of the time. Maybe 99% of the time.

The challenge does say

Inside function fun1 , assign 5 to oopsGlobal without using the var , let or const keywords.

The scoping rules of var are a little confusing. I suspect this is why it was omitted from the discussion.

Variables which are defined outside of a function block have Global scope.

This technically isn’t correct, let and const are not added to the global object.

let notGlobal = 'not global';
const alsoNotGlobal = ' also not global';

console.log(globalThis.notGlobal); // undefined
console.log(globalThis.alsoNotGlobal); // undefined

They are top-level variables, not global in the sense that var would be.

It really also should say “outside a function or a code block” as both let and const are block scoped.

1 Like

What do you mean let and const are block scoped?

let notGlobal = 'not global';
const alsoNotGlobal = ' also not global';

Are you saying that these 2 variables, declared the way they are, would not be available to functions located throughout the code?

no they wouldn’t be visible by any external block scope from where they were declared here there is a decent explanation

1 Like

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