freeCodeCamp Challenge Guide: Global Scope and Functions

Global Scope and Functions


Hints

Hint 1

The scope of a variable is its visibility; where in the code is the function available? Here is a list of the different scopes a variable can have.

  • Global scope: The variable is available throughout the code
  • Local scope: Available in only a certain area (like only within function)
  • Block scope: Available within an even more certain area (like an if-statement)

Your task is to understand how adding var (and not adding) before a variable name, can change the variable’s scope.

When you add var before the variable name, its scope is determined by where it is placed. Like so:

var num1 = 18; // Global scope
function fun() {
  var num2 = 20; // Local (Function) Scope
  if (true) {
    var num3 = 22; // Block Scope (within an if-statement)
  }
}

When you don’t, this is the result:

num1 = 18; // Global scope
function fun() {
  num2 = 20; // Global Scope
  if (true) {
    num3 = 22; // Global Scope
  }
}

Solutions

Solution 1 (Click to Show/Hide)
// Declare your variable here
var myGlobal = 10;

function fun1() {
  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);
}
7 Likes

Only you have to do is declare 2 variables with var and without var as follows. First, declare a variable for myGlobal and give them a value of 10 and another is oopsGlobal without var keyword and give them a value of 5. Then, add myGlobal variable into a function fun1. That’s it. :grinning:

Answer is:

// Declare your variable here
var myGlobal = 10;
oopsGlobal = 5;

function fun1(myGlobal) {
  // Assign 5 to oopsGlobal Here
  var output = "";
  if (typeof myGLobal != "undefined") {
output += "myGlobal: " + myGlobal;
  }
  if (typeof myGlobal != "undefiend") {
output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

// 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);
}
5 Likes

I completed this piece by doing the following:

I just made X=5 within the function of fun1

// Declare your variable here
var myGlobal=10;

function fun1(x) {
  // Assign 5 to oopsGlobal Here
x=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);
}

Another solution I created was:

// Declare your variable here
var myGlobal=10;
oopsGlobal=5;
function fun1(oopsGlobal) {
  // Assign 5 to oopsGlobal Here
oopsGlobal;  
}
// 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);
}

I’m not sure if this is the kind of asnwer they wanted though…

3 Likes

Hi Mike, I think when you declare a variable without ‘var’, it automatically becomes global.
So, even if you declared ‘oopsGlobal’ inside, its still global without the var.

1 Like

Your right Omar…! An variable outside of a “function” is a global variable. Even though function fun2, I don’t know the purpose of function fun2()?
// Declare your variable here
var myGlobal=10;
oopsGlobal=5;

function fun1(oopsGlobal) {
// Assign 5 to oopsGlobal Here
oopsGlobal;
}
// 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);
}

Withdrawn. Didn’t see var wasn’t listed on the answer.

Summary

It’s as simple as adding the two declarations. Do not bother with copying anything from fun(2). Just simply declare the variables in the two different ways.

My solution i tried out

var myGlobal= 10;// Declare your variable here

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);
}

4 Likes

This is how it worked for me!

// Declare your variable here
myGlobal = 10;
oopsGlobal = 5;

function fun1() {
// Assign 5 to oopsGlobal Here
var myGlobal = 10 ;
}

// 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);
}

1 Like