Global Scope and Functions: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions

Hello fellow freeCodeCampers,

I kinda understood the concept explained in this chapter about the Global Scope and Functions, or rather may I add the repercussions of not declaring variables at the correct place as intended. So I understood in general.

But what I’m not getting in the code is that from where did the function fun2() has been called? i.e., we have not called fun2() explicitly in the code for it to show the output as:
myGlobal: 10 oopsGlobal: 5
Because var output is function scoped to fun2() function, isn’t it?

Can someone from the FCC mentor please explain and clarify to me?

Hope the question made sense.
Many thanks in advance.

Cheers!!!

  **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(output);
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36

Challenge: Global Scope and Functions

Link to the challenge:

The function is called by the tests.

Thanks ArielLeslie for the confirmation and reply.

Cheers!!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.