Tell us what’s happening:
Your exercise seems to imply that by excluding the keyword var the variable you create inside a function that it becomes a global variable. When in fact oopsGlobal is already declared but simply not visible in the code.
Please add the oopsGlobal declaration on the code so it isn’t as confusing
Your code so far
// Declare the myGlobal variable below this line
var myGlobal = 10;
function fun1() {
oopsGlobal = 5;// Assign 5 to oopsGlobal Here
randomVar = 2;//this is wrong if you haven't declared it before
var randomVar = 2;// if I create a var here its Local
}
// Only change code above this line
function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
}
if (typeof randomVar!= "undefined") { //this one doesn't show up
output += " oopsGlobal: " + randomVar;
}
console.log(output);
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0
.
Challenge: Global Scope and Functions
Link to the challenge: