Skip to content

Commit 7bde9f9

Browse files
Added JavaScript concepts on Hoisting, Functions and Variable Environments, Window and this keyword, and Undefined vs Not Defined
1 parent 8968057 commit 7bde9f9

4 files changed

Lines changed: 234 additions & 0 deletions

File tree

06_Hoisting.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Episode 3 : Hoisting
2+
3+
## Code Example 01
4+
5+
```javascript
6+
var x = 7;
7+
8+
function getName() {
9+
console.log("Namaste JavaScript");
10+
}
11+
12+
getName();
13+
console.log(x);
14+
```
15+
16+
Output:
17+
18+
Namaste JavaScript
19+
7
20+
21+
<br>
22+
23+
## Code Example 02
24+
25+
```javascript
26+
getName(); // in most languages, both the lines which are above their declaration will give error. But not in JS though.
27+
console.log(x);
28+
29+
var x = 7;
30+
31+
function getName() {
32+
console.log("Namaste JavaScript");
33+
}
34+
```
35+
36+
Output:
37+
38+
Namaste JavaScript
39+
undefined
40+
41+
<br>
42+
43+
## Code Example 03
44+
45+
```javascript
46+
getName();
47+
console.log(x);
48+
49+
function getName() {
50+
console.log("Namaste JavaScript");
51+
}
52+
```
53+
54+
Output:
55+
56+
Namaste JavaScript
57+
Error: x is not defined // note that not defined here and "undefined" in
58+
sample 2 are totally different.
59+
60+
- Not defined: We have not initialised the value for variable anywhere in the entire code and in memory space.
61+
- Undefined: It is a placeholder that is assigned to a variable by the Javascript Engine until the variable is assigned with some other value.
62+
63+
**Hoisting** is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting _error_
64+
65+
<br>
66+
67+
## Code Example 04
68+
69+
```javascript
70+
function getName() {
71+
console.log("Namaste JavaScript");
72+
}
73+
74+
console.log(getName);
75+
```
76+
77+
Output:
78+
79+
f getName(){
80+
console.log("Namaste JavaScript);
81+
}
82+
83+
<br>
84+
85+
## Code Example 05
86+
87+
```javascript
88+
getName();
89+
console.log(x);
90+
console.log(getName);
91+
92+
var x = 7;
93+
94+
function getName() {
95+
console.log("Namaste JavaScript");
96+
}
97+
```
98+
99+
Output:
100+
101+
Namaste JavaScript
102+
103+
undefined
104+
105+
f getName(){
106+
console.log("Namaste JavaScript);
107+
}
108+
109+
<br>
110+
111+
## Code Example 06
112+
113+
```javascript
114+
console.log(getName);
115+
116+
var getName = function () {
117+
console.log("Namaste JavaScript");
118+
};
119+
120+
var getName = () => {
121+
// use fat arrow function
122+
console.log("Namaste JavaScript");
123+
};
124+
```
125+
126+
Output:
127+
128+
undefined //it is because arrow function behave as variable and not function.
129+
130+
---
131+
132+
**REASON OF WEIRDNESS**
133+
134+
- The answer lies in the Global Execution Context. In the memory phase, the variables will be initialized as _undefined_ and functions will get the whole function code in their memory.
135+
136+
- This is the reason why we are getting these outputs.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Episode 4 : Functions and Variable Environments
2+
3+
```javascript
4+
var x = 1;
5+
6+
a();
7+
8+
b(); // we are calling the functions before defining them. This will work properly, as seen in Hoisting.
9+
10+
console.log(x);
11+
12+
function a() {
13+
var x = 10;
14+
console.log(x);
15+
}
16+
17+
function b() {
18+
var x = 100;
19+
console.log(x);
20+
}
21+
```
22+
23+
Outputs:
24+
25+
10
26+
27+
100
28+
29+
1
30+
31+
### Code Flow
32+
33+
- The Global Execution Context (GEC) is created (the big box with Memory and Code subparts). Also GEC is pushed into Call Stack
34+
> Call Stack : GEC
35+
- In first phase of GEC (memory phase), variable `x:undefined` and `a` and `b` have their entire function code as value initialized
36+
- In second phase of GEC (execution phase), when the function is called, a new local EC is made. After `x = 1` assigned to GEC `x`, `a()` is called. So local EC for a is made inside code part of GEC.
37+
> Call Stack: [GEC, a()]
38+
- For local EC, a totally different `x` variable assigned `undefined` (`x` inside `a()`) in phase 1 , and in phase 2 it is assigned `10` and printed in console log. After printing, no more commands to run, so `a()` local EC is removed from both GEC and from Call stack
39+
> Call Stack: GEC
40+
- Cursor goes back to `b()` function call. Same steps repeat.
41+
> Call Stack :[GEC, b()] -> GEC (after printing yet another totally different x value as 100 in console log)
42+
- Finally GEC is deleted and also removed from call stack. Program ends.

08_window_And_this.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Episode 5: Window and this keyword
2+
3+
### Everywhere JS is run, it is done with a JS execution engine. For Chrome: v8
4+
5+
- Shortest JS program is nothing but an Empty JS file.
6+
- Even for this program, JS engine does a lot of the things behind the scenes.
7+
- It creates the GEC, the "window" and the _this_ variable.
8+
- Window is a big global object that has a lot of functions and variables, which is created along with global execution context.These functions and varibles are created by the JavaScript Engine and into the global scope. All of these can be accessed from anywhere in the program.
9+
- At the global level, _this_ points to _window_ object.
10+
> this === window -> true (at global level)
11+
12+
_Whenever a JavaScript program is run, a **global object** is created, a **global execution context** is created and along with it a **this** varible is also created._
13+
14+
```javascript
15+
var a = 10; // not inside any fxn. So global object
16+
17+
function b() {
18+
// this fxn not inside any function. So global.
19+
var x = 5; // not global
20+
}
21+
22+
console.log(window.a); //gives us "a" value
23+
24+
console.log(this.a); //this points to window so it returns "a" value
25+
26+
console.log(a); //also gives same "a" value. (if we don't put any . in front of variable, it **assumes variable is in global space**
27+
28+
console.log(x); // x is not defined. (tries to find x inside global space, but it isn't there)
29+
```
30+
31+
- Global space is anything in JS which isn't inside a function. All these global objects will be present inside the windows schema. But non globals ones won't be there (here, x)
32+
- When a GEC is made, _this_ is also created with it (even for functional(local) EC). Global object provided by the browser engine is the window, so _this_ points to window.

09_undefined_vs_not_defined.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Episode 6: Undefined vs Not Defined
2+
3+
- In first phase (memory allocation) JS assigns each variable to a placeholder called _undefined_
4+
- _undefined_ is when memory is allocated for the variable, but no value is assigned yet.
5+
- If an object/variable is not even declared/found in mem alloc phase, and tried to access it then it is _Not defined_
6+
7+
> When variable is declared but not assigned value, its current value is undefined. But when the variable itself is not declared but called in code, then it is not defined.
8+
9+
```javascript
10+
console.log(x);
11+
12+
var x = 25;
13+
14+
console.log(x);
15+
16+
console.log(a);
17+
```
18+
19+
undefined
20+
25
21+
Uncaught ReferenceError: a is not defined
22+
23+
- JS is a loosely-typed / weakly-typed language. It doesn't attach variables to any datatype. We can say var a = 5, and then change the value to bool (a = true) or string (a = 'hello') later on.
24+
- **Never** assign _undefined_ to a variable manually. Let it happen on it's own accord.

0 commit comments

Comments
 (0)