You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Notes/3-Hoisting.md
+33-28
Original file line number
Diff line number
Diff line change
@@ -16,15 +16,15 @@ console.log(x);
16
16
17
17
Output:
18
18
19
-
> Namaste JavaScript
19
+
> Namaste JavaScript
20
20
21
-
> 7
21
+
> 7
22
22
23
23
```
24
24
// code example 2
25
25
26
-
getName(); // in most languages, both lines which are above their declaration will give error. Not in JS though.
27
-
console.log(x);
26
+
getName(); // in most languages, both lines which are above their declaration will give error. Not in JS though.
27
+
console.log(x);
28
28
29
29
var x = 7;
30
30
@@ -34,10 +34,11 @@ function getName(){
34
34
35
35
```
36
36
37
-
Output:
38
-
> Namaste JavaScript
37
+
Output:
38
+
39
+
> Namaste JavaScript
39
40
40
-
> undefined
41
+
> undefined
41
42
42
43
```
43
44
// code example 3
@@ -53,14 +54,18 @@ function getName(){
53
54
54
55
Output:
55
56
56
-
> Namaste JavaScript
57
+
> Namaste JavaScript
57
58
58
-
> Error: x is not defined // note that not defined here and "undefined" in sample 2 are totally different.
59
+
> Error: x is not defined // note that not defined here and "undefined" in
60
+
> sample 2 are totally different.
59
61
60
-
- Not defined: We have not initialised the value for variable anywhere in the entire code and in memory space.
61
-
- Undefined:
62
+
- Not defined: We have not initialised the value for variable anywhere in the
63
+
entire code and in memory space.
64
+
- Undefined: It is a placeholder that is assigned to a variable by the
65
+
Javascript Engine until the variable is assigned with some other value.
62
66
63
-
__Hoisting__ is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting *error*
67
+
**Hoisting** is a concept which enables us to extract values of variables and
68
+
functions even before initialising/assigning value without getting _error_
64
69
65
70
```
66
71
@@ -78,9 +83,10 @@ console.log(getName)
78
83
Output:
79
84
80
85
> f getName(){
86
+
81
87
console.log("Namaste JavaScript);
82
-
}
83
88
89
+
}
84
90
85
91
```
86
92
@@ -99,13 +105,16 @@ function getName(){
99
105
```
100
106
101
107
Output:
102
-
> Namaste JavaScript
103
108
104
-
> undefined
109
+
> Namaste JavaScript
110
+
111
+
> undefined
112
+
113
+
> f getName(){
105
114
106
-
> f getName(){
107
115
console.log("Namaste JavaScript);
108
-
}
116
+
117
+
}
109
118
110
119
```
111
120
// code example 6
@@ -116,26 +125,22 @@ var getName = function () {
116
125
console.log("Namaste JavaScript");
117
126
}
118
127
119
-
var getName = () => { // use fat arrow function
128
+
var getName = () => { // use fat arrow function
120
129
console.log("Namaste JavaScript");
121
130
}
122
131
123
132
```
124
133
125
134
Output:
126
135
127
-
> undefined //it is because they behave as variable and not function.
136
+
> undefined //it is because they behave as variable and not function.
128
137
129
138
---
130
139
131
-
__REASON OF WEIRDNESS__
132
-
133
-
* The answer lies in the Global Exection Context. In the memory phase, the variables will be initialized as *undefined* and functions will get the whole function code in their memory.
134
-
135
-
* This is the reason why we are getting these outputs.
136
-
137
-
138
-
139
-
140
+
**REASON OF WEIRDNESS**
140
141
142
+
- The answer lies in the Global Exection Context. In the memory phase, the
143
+
variables will be initialized as _undefined_ and functions will get the whole
144
+
function code in their memory.
141
145
146
+
- This is the reason why we are getting these outputs.
0 commit comments