Basic JavaScript - Global Scope and Functions

Tell us what’s happening:
Describe your issue in detail here.
Why do I get "undefined when I use console.log(typeof oopsGlobal) but at the same time oopsGlobal != undefined
you can see in the result what the code says.

I understand what the problem is and know how to avoid it but I don’t understand why I see a contradiction in the results.
Your code so far

// Declare the myGlobal variable below this line
let myGlobal = 10;

function fun1() {
  // Assign 5 to oopsGlobal here
  oopsGlobal = 5;
 console.log(typeof oopsGlobal + " 1"); // I put number 1, 2 and 3 so I can clearly see which result comes from which console.log
}

// Only change code above this line

function fun2() {
  let output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") { // so if this shows then oopsGlobal is defined
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}
console.log(typeof oopsGlobal+ " 2"); // here it says it's undefined
console.log(typeof myGlobal+ " 3");

Your browser information:

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

Challenge: Basic JavaScript - Global Scope and Functions

Link to the challenge:

1 Like

Are you referring to this line?

console.log(typeof oopsGlobal+ " 2"); // here it says it's undefined

When that line is executed it is the first time the variable oopsGlobal is actually accessed in the code, so it will be undefined.

Add the following below that line:

fun1();
console.log(typeof oopsGlobal+ " 22");

You’ll see a different result once you run the fun1 function because fun1 has actually given it a value.

1 Like

It is because the oopsGlobal variable is a local variable for fun1 and it does not have a global scope, which means it is not accessible outside the function

If you assign oopsGlobal outside the fun1() it will give you “typeof number”.

I hope you will understand and I have cleared your doubt

Yes, I think that is the answer I was looking for. Thanks for clearing that up. While waiting for the answer I was playing with it in codepen and saw the same result.

This isn’t quite right. Because the variable is not being declared with either let or const then it does have global scope. That is the point of this challenge, to convince you to always use either let or const. The challenge is not using those on purpose to show you what will happen if you don’t.

2 Likes

Taqi42, I think you misunderstood my question.
The problem with this javascript exercise is in fact the global or local scope. By not using let or const I made it a global variable, not a local one .But my problem was with what seemed like a contradiction, luckily bbsmooth gave me the solution. Thank you for helping out.

1 Like

Sorry I misunderstood thanks for correcting me :innocent:

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