Explain how this works

Tell us what’s happening:
Describe your issue in detail here.
Even though I have done it but I cannot understand what it does and explain me to understand for more

  **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;
}
console.log(output);
}
  **Your browser information:**

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

Challenge: Global Scope and Functions

Link to the challenge:

in first line you have declared and initialized a global variable ( myGlobale = 10 ; )

function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal = 5;
}

from this if you are expecting to access oopsGlobal then you can not because you have not declared it and you are declaring it in the function ,which means you can not access it . because when we make variable inside function .that variable have only function scope which means we can not access that function outside of that function.
so you should write this code like this if you are expecting output .

    var myGlobal = 10 ;
    var  oopsGlobal = 5;
    function fun2() {
        var output = "";
        if (typeof(myGlobal) != "undefined") {
            output += "myGlobal: " + myGlobal;
        }
       if (typeof(oopsGlobal) != "undefined") {
            output += " oopsGlobal: " + oopsGlobal;
        }
        return output;
    }
    console.log(fun2());

// output = myGlobale: 10 oopsGlobal : 5

Sorry if i am misreading your words, but its actually showing how you can accidentally make what should be a variable that is only scoped to a function accidentally global.

If you do not declare a variable with var javascript by default will automatically make it a global variable, even if its in a function, which can cause bugs if you don’t catch it.

so in this case even tho oopsGlobal is in function fun1() and should only be seen inside that function, function fun2() can actually see oopsGlobal and is returning oopsGlobal: 5, when it shouldn’t actually be able to see oopsGlobal at all in function fun1()

1 Like

But here, fun1() is not invoked. So oopsGlobal value is undefined.
So oopsGlobal is set as a global variable. But it’s value is not 5 unless fun1() is invoked.

There are some comments here misunderstanding variable scope.

{
// any variable here is a global variable if not declared with var, let, or const
// even if the curly brackets are for a function, it does not need to be invoked/called
}

Your code logs out:
myGlobal: 10 oopsGlobal: 5
showing the error of not using var, let or const in the curly brackets and the unintended consequences that may cause errors in your code. This is why the variable is named oppsGlobal, because opps it should not be global.

https://www.w3schools.com/js/js_scope.asp

1 Like

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