-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_scope2.js
More file actions
173 lines (137 loc) · 3.33 KB
/
Copy pathvariable_scope2.js
File metadata and controls
173 lines (137 loc) · 3.33 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Please predict the output of the following programs, and explain why they output what they do.
// 1.
// function say() {
// if (false) {
// var a = 'hello from inside a block';
// }
// console.log(a);
// }
// say();
// logs: undefined
// the var variable a is hoisted in creation and assigned value of undefined, but the statement assigning the string is never executed.
// Need to note, var is FUNCTION-LEVEL and not block-level, so hoisted above if(false) statement
//2.
// function say() {
// if (false) {
// let a = 'hello from inside a block';
// }
// console.log(a);
// }
// say();
// log: reference error. The let variable is is block level and hoisted to top of block, but never executed
//3.
// function hello() {
// a = 'hello';
// console.log(a);
// if (false) {
// var a = 'hello again';
// }
// }
// hello();
// console.log(a);
// logs:
// hello
// referenc error <- var on line 6 is FUNCTION LEVEL scope and hosited to top of function. Therefore not accessible
// 4.
// function hello() {
// a = 'hello';
// console.log(a);
// if (false) {
// let a = 'hello again';
// }
// }
// hello();
// console.log(a);
// logs:
// hello
// hello <- let a is blocked scoped and therefore a has no globaal scope BUT the original a on line two is not delcared anywhere and so by default assigned to global object
// 5.
// var a = 'hello';
// for (var index = 0; index < 5; index += 1) {
// var a = index;
// }
// console.log(a);
// logs: 4 <- the var a in the for statement gets hoisted to global scope since it is FUNCTION scoped and not block scoped
// 6.
// let a = 'hello';
// for (let index = 0; index < 5; index += 1) {
// let a = index;
// }
// console.log(a);
// logs: hello <- the let variable in the for statement is block scoped and therefore shadowed by the globaly scoped a variable on line 1
// 7.
// let a = 1;
// function foo() {
// a = 2;
// let bar = function() {
// a = 3;
// return 4;
// };
// return bar();
// }
// console.log(foo());
// console.log(a);
// logs:
// 4 <- retgurned by bar()
// 3 <- the global variable let a is reassigned to value 2 and then 3
// 8.
// var a = 'global';
// function checkScope() {
// var a = 'local';
// const nested = function() {
// var a = 'nested';
// let superNested = () => {
// a = 'superNested';
// return a;
// };
// return superNested();
// };
// return nested();
// }
// console.log(checkScope());
// console.log(a);
// logs:
// supernested
// global
// 9.
// let a = 'outer';
// let b = 'outer';
// console.log(a);
// console.log(b);
// setScope(a);
// console.log(a);
// console.log(b);
// function setScope(foo) {
// foo = 'inner';
// b = 'inner';
// }
// logs:
// outer
// outer
// outer <- function arguments become local variables to the function and value of the original variable is not changed
// inner
// 10.
// let total = 50;
// let increment = 15;
// function incrementBy(increment) {
// total += increment;
// }
// console.log(total);
// incrementBy(10);
// console.log(total);
// console.log(increment);
// logs:
// 50
// 65
// 15
// 11.
let a = 'outer';
console.log(a);
setScope();
console.log(a);
var setScope = function () {
a = 'inner';
};
// logs:
// outer
// Error <- the var setScope is hoisted and assigned undefined and function has not yet been assigned when called on line 4