-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoops_Scope.js
More file actions
47 lines (38 loc) · 1.48 KB
/
Loops_Scope.js
File metadata and controls
47 lines (38 loc) · 1.48 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
//following loops iterates and for 5 times adds/pushes a function to an array called operations. function = ()=>
//function console logs the loop index variable i. The expected output is 0,1,2,3,4. But this returns 5,5,5,5,5
//this is the result of the use of var. var declarations are hoisted. so in the for loop, i is still visible, but
//equals to 5 and every reference to i the function is going to use this value. Use let to avoid some of the weird
//things about var declarations.
/*
const operations = []
for (var i = 0; i < 5; i++) {
operations.push(() => {
console.log(i)
})
}
for (const operation of operations) {
operation()
} */
//changing var to let returns expected results because on every loop iteration, i is created as a new variable
//each time and every function added to the operations array gets its own copy of i.
const operations = []
for (let i = 0; i < 5; i++) {
operations.push(() => {
console.log(i)
})
}
for (const operation of operations) {
operation()
}
//if determined to use var, another way to solve problem is to use Immediately Invoked Function Expression (IIFE).
//with IIFE, you can wrap the entire function and bind i to it. Since in this way you’re creating a function that \
//immediately executes, you return a new function from it, so we can execute it later.
const operationz = []
for (var i = 0; i < 5; i++) {
operationz.push(((j) => {
return () => console.log(j)
})(i))
}
for (const operation of operationz) {
operation()
}