/Tell us what’s happening:
Describe your issue in detail here.
It is given that if variables are declared without let or const , it is automatically created in global scope, my doubt here is that , inside function fun2() when console.log(typeof(oopsGlobal)) is called we get value as number, but when the same is called outside of a function it returns the type of the same variable as “undefined”, why is it happening? Ideally it has to be a global variable right? so typeof(oopsGlobal) has to be number right? why is it returning type as “undefined” when called outside a function?
**Your code so far**
// 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(typeof(oopsGlobal)); //returns type as number
}
console.log(typeof(oopsGlobal)); //returns type as undefined
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Global Scope and Functions
Link to the challenge: