-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal Scope and Functions
33 lines (26 loc) · 1.12 KB
/
Global Scope and Functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* Global Scope and Functions
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.
Using var, declare a global variable myGlobal outside of any function. Initialize it with a value of 10.
Inside function fun1, assign 5 to oopsGlobal without using the var keyword. */
// Declare your variable here
var 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); // Output equals myGlobal: 10 oopsGlobal: 5
}
// End Global Scope and Functions Commit