Please someone explain this

Tell us what’s happening:
If the variables declared without var goes to global scope then when I am writing “console.log(typeof oopsGlobal)” outside all the function it shows “undefined” and if I write “console.log(typeof oopsGlobal)” inside any function it shows “number”.
Please someone explain this…

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/84.0.4147.135 Safari/537.36.

Challenge: Global Scope and Functions

Link to the challenge: