Tell us what’s happening:
Hello,
Okay so I don’t actually have a problem completing the task, I’m just a bit unclear about the wording of it. It says " Using var, declare a global variable named myGlobal outside of any function. ", I thought it was a global variable if you declared it without the ‘var’? Is this a typo or am I unclear about the explanation above the task? 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/89.0.4389.128 Safari/537.36.
I read a little about variable scopes on w3schools, and I think I’m a little clearer on the topic now. Where the variable is declared can also determine if it’s local or global and not the way it is declared i.e if declared in a function it is local to that function and is not accessible outside that function but if the variable is declared outside a function it’s a global variable which is accessible anywhere in your code. So even if
is declared without a ‘var’ it’s still going to be local because it is within a function.