Global Scope and Fnctn

Tell us what’s happening:

Your code so far


// Declare your variable here
var myGlobal = 10;
function fun1(oopsGlobal) {
  // 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/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions

The objective of the challenge is to teach you about global variables.

You properly declared the global variable myGlobal but you did not declare the other global variable oopsGlobal.

To declare a global variable correctly we use var keyword but for this exercise we want to declare oopsGlobal incorrectly without using var.

Hopefully this explanation helps you figure out what you need to add to pass the challenge…

Can u pls tell me directly what I need to add further in this.
Thanx in advnce.

Answer this:

how do I declare a global variable?

If you’re not sure, re-read the exercise.

The problem is that you are not assigning 5 to a variable, you are assigning it to a parameter:

function myFunc(parameter1, parameter2, etc) {
  parameter1 = 5;
}

The point of the exercise is to realise that if you do not declare a variable using the var keyword, the variable will have global scope:

function myFunc() {
  var myLocalVariable = 5; // has local scope
  myGlobalVaraible = 5; //has global scope
}

console.log(myLocalVariable); // output: Reference Error: myLocalVariable is not defined
console.log(myGlobalVariable); // output: 5

In order to complete the exercise, all you need to do is stop defining oopsGlobal as a parameter, the line should look like this:

function fun1() {

The rest of your code is correct.

Hope this helps :slight_smile: