Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path048-hoisting.js
More file actions
Latest commit
117 lines (89 loc) · 2.66 KB
/
Copy path048-hoisting.js
File metadata and controls
117 lines (89 loc) · 2.66 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
// SECTION 1: JAVASCRIPT ENGINE
// - Parsing
// - Execution
// You write JavaScript code → The engine parses it → Creates an execution context → Executes the code.
// SECTION 2: EXECUTION CONTEXT
// 1. Global Execution Context (GEC)
// 2. Function Execution Context (FEC)
// const name = "Alex";
// const greet = () => {
// const message = "Hello";
// console.log(message + " " + name);
// };
// greet();
// SECTION 3: PHASES OF EXECUTION CONTEXT
// Phase 1: Memory Creation Phase (Creation Phase)
// Phase 2: Code Execution Phase
console.log(age);// What do you think this will output?
constage=25;
console.log(greeting);// undefined
vargreeting="Hello World";
console.log(greeting);// "Hello World"
// SECTION 4: HOISTING
// Hoisting with var:
console.log(x);// undefined
varx=10;
console.log(x);// 10
varx;// Declaration hoisted
console.log(x);// undefined
x=10;// Initialization stays here
console.log(x);// 10
// Hoisting with let and const:
console.log(y);// ReferenceError: Cannot access 'y' before initialization
lety=20;
// SECTION 5: FUNCTION HOISTING
// Function Declarations:
sayHello();// "Hello!" - This works!
functionsayHello(){
console.log("Hello!");
}
// Function Expressions with var:
sayHi();// TypeError: sayHi is not a function
varsayHi=function(){
console.log("Hi!");
};
// Function Expressions with const:
greetUser();// ReferenceError: Cannot access 'greetUser' before initialization
constgreetUser=function(){
console.log("Greetings!");
};
// Arrow Functions:
welcome();// ReferenceError: Cannot access 'welcome' before initialization
constwelcome=()=>{
console.log("Welcome!");
};
// SECTION 6: PRACTICAL EXAMPLE
console.log("1:",a);// undefined
console.log("2:",calculate(5));// 25
vara=10;
functioncalculate(num){
constresult=num*num;
returnresult;
}
console.log("3:",a);// 10
console.log("4:",multiply(4,5));// ReferenceError
constmultiply=(x,y)=>{
returnx*y;
};
// SECTION 7: THE CALL STACK
constfirst=()=>{
console.log("First function");
second();
console.log("First function again");
};
constsecond=()=>{
console.log("Second function");
third();
console.log("Second function again");
};
constthird=()=>{
console.log("Third function");
};
first();
// Output
// First function
// Second function
// Third function
// Second function again
// First function again
// Temporal dead zone