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