-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path02-scoping.js
More file actions
36 lines (28 loc) · 989 Bytes
/
02-scoping.js
File metadata and controls
36 lines (28 loc) · 989 Bytes
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
// ------------------------------------
// given
let letGlobalScope = 20;
// !!! avoid this type of declaration !!!
var varGlobalScope = [ 3, 4, 5, 6, 7 ];
console.log(`usual visibility letGlobalScope: ${letGlobalScope} varGlobalScope: ${varGlobalScope}`);
// then
(tempFunction = () => {
console.log(`visibility inside function letGlobalScope: ${letGlobalScope} varGlobalScope: ${varGlobalScope}`);
})();
// ------------------------------------
// given
(function(){
console.log(`declare *innerVariableLet* inside function`);
let innerVariableLet =10;
})();
{
console.log(`declare *innerVariableVar* inside anonymous block ${innerVariableVar}`);
// !!! avoid this type of declaration !!!
var innerVariableVar = 20;
}
// then
try{
console.log(`read *innerVariableLet* from outer scope: ${innerVariableLet}`) // variable not found
}catch(ex){
console.log(ex.message)
}
console.log(`read innerVariableVar from outer scope : ${innerVariableVar}`);