-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope and closures.js
101 lines (70 loc) · 2.18 KB
/
scope and closures.js
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
// Global Scope
let globalVar = "I am global!";
function printGlobalVar() {
console.log(globalVar); // Accessible here
}
printGlobalVar(); // Output: I am global!
// Function Scope
function myFunction() {
let functionVar = "I am local to this function!";
console.log(functionVar); // Accessible here
}
myFunction();
// console.log(functionVar); // Uncaught ReferenceError: functionVar is not defined
// Block Scope (ES6)
if (true) {
let blockScopedVar = "I am block-scoped!";
console.log(blockScopedVar); // Accessible here
}
// console.log(blockScopedVar); // Uncaught ReferenceError: blockScopedVar is not defined
// Hoisting
console.log(x); // Output: undefined (due to hoisting)
var x = 5;
console.log(y); // Output: ReferenceError (let is not hoisted like var)
let y = 10;
// Lexical Scope
let outerVar = "Outer";
function outerFunction() {
let innerVar = "Inner";
function innerFunction() {
console.log(outerVar); // Can access outer variables
console.log(innerVar); // Can access variables in the same scope
}
innerFunction();
}
outerFunction();
// innerFunction(); // Uncaught ReferenceError: innerFunction is not defined
// Closures in JavaScript
function outer() {
let outerVar = "I am from the outer scope";
return function inner() {
console.log(outerVar); // Can access the outerVar even after outer has returned
};
}
const closureFunction = outer();
closureFunction(); // Output: I am from the outer scope
// More Practical Closure Example:
function counter() {
let count = 0;
return function() {
count++;
return count;
};
}
const increment = counter();
console.log(increment()); // Output: 1
console.log(increment()); // Output: 2
console.log(increment()); // Output: 3
// Closures in Loops (Common Pitfall and Fix)
// The Pitfall with var:
for (var i = 1; i <= 3; i++) {
setTimeout(function() {
console.log(i); // Output: 4, 4, 4 (due to `var` being function-scoped)
}, 1000);
}
// Fix with let:
for (let i = 1; i <= 3; i++) {
setTimeout(function() {
console.log(i); // Output: 1, 2, 3 (due to `let` being block-scoped)
}, 1000);
}