I found this challenge to be more confusing than it probably should have been.
Specifically regarding the following statement: " Variables which are declared without the let
or const
keywords are automatically created in the global
scope."…
Since this statement did not also mention var, this led me to believe that variables declared using var automatically become Globally scoped, even when located within a function.
After doing some googling, reading the hints and watching the video, I learned that variables can be declared without using any of the var, let or const keywords. Which I did not know, or at least dont remember being covered til now.
So now I know that variables can be declared without using those 3 keywords and that is the scenario in which a variable becomes automatically Globally scoped.
Maybe the challenge was designed that way on purpose to force the habit of additional research. I just feel like this challenge would be a lot less confusing if var had also been mentioned in the above statement I referenced.
// 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(output);
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0
Challenge: Basic JavaScript - Global Scope and Functions
Link to the challenge: