-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy path1_javascript_scope.js
More file actions
80 lines (74 loc) · 2.52 KB
/
1_javascript_scope.js
File metadata and controls
80 lines (74 loc) · 2.52 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Given the JavaScript code,
* what will the browser's console show?
*
* ** What I learned:
*
* ** Scopes in JavaScript
* (1) function-level scope
* : functions create new scopes in JavaScript
* (2) NO if-scope or loop-scope
* : if statements and for loops do not create a new scope
* (also for Python <-> Java and C: creates new scope)
*
* * undefined (<-> ReferenceError)
* = variable declared but not yet initialized to specific value
* -> by default, variables declared but not yet assigned are initialized to 'undefined'
*
* ** Hoisting
* : raising things to the top
* ; in JavaScript, variable & function DECLARATIONS are hoisted to the top of the current scope
* however, variable & function ASSIGNMENTS are not
*
* * var, function, function* declaration === 'undefined'
* * const, let, class declaration === uninitialized => access throws ReferenceError
*
* => both sides are hoisted (declared at its current scope)
* BUT, (var, function, function*) can be accessed and return 'undefined'
* while (const, let, class) cannot be accessed at all (throws ReferenceError)
*
* => hoisting can cause UNEXPECTED BEHAVIOR
* so a good way to keep things clear is to
* ALWAYS DELCARE VARIABLES AT THE TOP OF THE SCOPE!!
*
*/
var text = 'outside';
function logIt() {
console.log(text);
var text = 'inside'; // declaration hoisted to top of logIt()'s scope
}
logIt(); // log: undefined
/**
* function-level scope
* : varInFunction is only available in function's scope
*/
function setVar() {
var varInFunction = 'inside a function';
}
setVar();
console.log(varInFunction); // throws 'ReferenceError: varInFunction is not defined'
/**
* NO if-scope or loop-scope
* : varInIf is available in global scope
*/
if (true) {
var varInIf = 'inside an if statement';
}
console.log(varInIf); // log: 'inside an if statement'
/**
* declaration Vs. assignment
*/
var unicorn; // declaration
console.log(unicorn); // log: undefined (X referenceError)
unicorn = 'sparkly'; // assignment
var centaur = 'clouds'; // declaration & assignment
/** Hoisting changes the original
* problem code to below code
*/
var text = 'outside';
function logIt() {
var text; // hoisted from below text variable;
console.log(text); // undefined (bcuz text declared but not assigned yet)
text = 'inside';
}
logIt(); // log: undefined