Tell us what’s happening:
As per the instruction var Myglobal should declare before function but while doing so the code is not getting executed and showing error. But while its defined inside the function its getting executed and its running and showing the output.
What is correct way declare and why this error is coming?
Your code so far
// Declare your variable here
function fun1() {
// Assign 5 to oopsGlobal Here
var myGlobal=10;
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:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8
.
Link to the challenge:
https://www.freecodecamp.org/challenges/global-scope-and-functions