# freeCodeCamp Challenge Guide: Global Scope and Functions

freeCodeCamp Challenge Guide: Global Scope and Functions
0

#1

In JavaScript, `scope` refers to the visibility of variables. Variables which are defined outside of a function block have `Global` scope. This means, they can be seen everywhere in your JavaScript code.

Variables which are used without the `var` keyword are automatically created in the `global` scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with `var`.

How can a user declare a variable within a function if established methods don't work?
#2

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.

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

#3

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âŚ

#4

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.

#5

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

#6

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.

#7

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

"Need Help" Global Scope and Functions 2018
#8

This is how it worked for me!

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