Skip to content

Commit 681c2e3

Browse files
committed
fix: 🐛 notes alignment and typo
1 parent 5c0df47 commit 681c2e3

25 files changed

+1423
-1343
lines changed

dist/lectures.html

+130-130
Large diffs are not rendered by default.

dist/namaste-javascript-notes.pdf

7.33 KB
Binary file not shown.

notes/lectures.md

+235-210
Large diffs are not rendered by default.

notes/season-1/lecture-02.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
# Episode 2 : How JS is executed & Call Stack
22

3-
* When a JS program is ran, a **global execution context** is created.
3+
- When a JS program is ran, a **global execution context** is created.
44

5-
* The execution context is created in two phases.
6-
* Memory creation phase - JS will allocate memory to variables and functions.
7-
* Code execution phase
5+
- The execution context is created in two phases.
6+
7+
- Memory creation phase - JS will allocate memory to variables and functions.
8+
- Code execution phase
9+
10+
- Let's consider the below example and its code execution steps:
811

9-
* Let's consider the below example and its code execution steps:
1012
```js
1113
var n = 2;
1214
function square(num) {
13-
var ans = num * num;
14-
return ans;
15+
var ans = num * num;
16+
return ans;
1517
}
1618
var square2 = square(n);
1719
var square4 = square(4);
1820
```
21+
1922
The very **first** thing which JS does is **memory creation phase**, so it goes to line one of above code snippet, and **allocates a memory space** for variable **'n'** and then goes to line two, and **allocates a memory space** for **function 'square'**. When allocating memory **for n it stores 'undefined'**, a special value for 'n'. **For 'square', it stores the whole code of the function inside its memory space.** Then, as square2 and square4 are variables as well, it allocates memory and stores 'undefined' for them, and this is the end of first phase i.e. memory creation phase.
2023

2124
So O/P will look something like
@@ -24,7 +27,7 @@ So O/P will look something like
2427

2528
Now, in **2nd phase** i.e. code execution phase, it starts going through the whole code line by line. As it encounters var n = 2, it assigns 2 to 'n'. Until now, the value of 'n' was undefined. For function, there is nothing to execute. As these lines were already dealt with in memory creation phase.
2629

27-
Coming to line 6 i.e. **var square2 = square(n)**, here **functions are a bit different than any other language. A new execution context is created altogether.** Again in this new execution context, in memory creation phase, we allocate memory to num and ans the two variables. And undefined is placed in them. Now, in code execution phase of this execution context, first 2 is assigned to num. Then var ans = num * num will store 4 in ans. After that, return ans returns the control of program back to where this function was invoked from.
30+
Coming to line 6 i.e. **var square2 = square(n)**, here **functions are a bit different than any other language. A new execution context is created altogether.** Again in this new execution context, in memory creation phase, we allocate memory to num and ans the two variables. And undefined is placed in them. Now, in code execution phase of this execution context, first 2 is assigned to num. Then var ans = num \* num will store 4 in ans. After that, return ans returns the control of program back to where this function was invoked from.
2831

2932
![Execution Context Phase 2](/assets/phase2.jpg "Execution Context")
3033

@@ -34,11 +37,11 @@ So the **final diagram** before deletion would look something like:
3437

3538
![Execution Context Phase 2](/assets/final_execution_context.jpg "Execution Context")
3639

37-
* Javascript manages code execution context creation and deletion with the the help of **Call Stack**.
40+
- Javascript manages code execution context creation and deletion with the the help of **Call Stack**.
3841

39-
* Call Stack is a mechanism to keep track of its place in script that calls multiple function.
42+
- Call Stack is a mechanism to keep track of its place in script that calls multiple function.
4043

41-
* Call Stack maintains the order of execution of execution contexts. It is also known as Program Stack, Control Stack, Runtime stack, Machine Stack, Execution context stack.
44+
- Call Stack maintains the order of execution of execution contexts. It is also known as Program Stack, Control Stack, Runtime stack, Machine Stack, Execution context stack.
4245

4346
<hr>
4447

notes/season-1/lecture-03.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
# Episode 3 : Hoisting in JavaScript (variables & functions)
22

3-
* Let's observe the below code and it's explaination:
3+
- Let's observe the below code and it's explaination:
4+
45
```js
56
getName(); // Namaste Javascript
67
console.log(x); // undefined
78
var x = 7;
89
function getName() {
9-
console.log("Namaste Javascript");
10+
console.log("Namaste Javascript");
1011
}
1112
```
1213

13-
* It should have been an outright error in many other languages, as it is not possible to even access something which is not even created (defined) yet But in JS, We know that in memory creation phase it assigns undefined and puts the content of function to function's memory. And in execution, it then executes whatever is asked. Here, as execution goes line by line and not after compiling, it could only print undefined and nothing else. This phenomenon, is not an error. However, if we remove var x = 7; then it gives error. Uncaught ReferenceError: x is not defined
14+
- It should have been an outright error in many other languages, as it is not possible to even access something which is not even created (defined) yet But in JS, We know that in memory creation phase it assigns undefined and puts the content of function to function's memory. And in execution, it then executes whatever is asked. Here, as execution goes line by line and not after compiling, it could only print undefined and nothing else. This phenomenon, is not an error. However, if we remove var x = 7; then it gives error. Uncaught ReferenceError: x is not defined
1415

15-
* **Hoisting** is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting error and this is happening due to the 1st phase (memory creation phase) of the Execution Context.
16+
- **Hoisting** is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting error and this is happening due to the 1st phase (memory creation phase) of the Execution Context.
1617

17-
* So in previous lecture, we learnt that execution context gets created in two phase, so even before code execution, memory is created so in case of variable, it will be initialized as undefined while in case of function the whole function code is placed in the memory. Example:
18+
- So in previous lecture, we learnt that execution context gets created in two phase, so even before code execution, memory is created so in case of variable, it will be initialized as undefined while in case of function the whole function code is placed in the memory. Example:
1819

1920
```js
2021
getName(); // Namaste JavaScript
2122
console.log(x); // Uncaught Reference: x is not defined.
2223
console.log(getName); // f getName(){ console.log("Namaste JavaScript); }
23-
function getName(){
24-
console.log("Namaste JavaScript");
24+
function getName() {
25+
console.log("Namaste JavaScript");
2526
}
2627
```
2728

28-
* Now let's observe a different example and try to understand the output.
29+
- Now let's observe a different example and try to understand the output.
30+
2931
```js
3032
getName(); // Uncaught TypeError: getName is not a function
3133
console.log(getName);
3234
var getName = function () {
33-
console.log("Namaste JavaScript");
34-
}
35+
console.log("Namaste JavaScript");
36+
};
3537
// The code won't execute as the first line itself throws an TypeError.
3638
```
3739

notes/season-1/lecture-04.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ Outputs:
2727
2828
## Code Flow in terms of Execution Context
2929

30-
* The Global Execution Context (GEC) is created (the big box with Memory and Code subparts). Also GEC is pushed into Call Stack
30+
- The Global Execution Context (GEC) is created (the big box with Memory and Code subparts). Also GEC is pushed into Call Stack
3131

3232
> Call Stack : GEC
3333
34-
* In first phase of GEC (memory phase), variable x:undefined and a and b have their entire function code as value initialized
34+
- In first phase of GEC (memory phase), variable x:undefined and a and b have their entire function code as value initialized
3535

36-
* In second phase of GEC (execution phase), when the function is called, a new local Execution Context is created. After x = 1 assigned to GEC x, a() is called. So local EC for a is made inside code part of GEC.
36+
- In second phase of GEC (execution phase), when the function is called, a new local Execution Context is created. After x = 1 assigned to GEC x, a() is called. So local EC for a is made inside code part of GEC.
3737

3838
> Call Stack: [GEC, a()]
3939
40-
* 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
40+
- 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
4141

4242
> Call Stack: GEC
4343
44-
* Cursor goes back to b() function call. Same steps repeat.
44+
- Cursor goes back to b() function call. Same steps repeat.
4545

4646
> Call Stack :[GEC, b()] -> GEC (after printing yet another totally different x value as 100 in console log)
4747
48-
* Finally GEC is deleted and also removed from call stack. Program ends.
48+
- Finally GEC is deleted and also removed from call stack. Program ends.
4949

50-
* reference:
50+
- reference:
5151

5252
![Execution Context Phase 1](/assets/function.jpg "Execution Context")
5353

@@ -56,4 +56,4 @@ Outputs:
5656
Watch Live On Youtube below:
5757

5858
<a href="https://www.youtube.com/watch?v=gSDncyuGw0s&ab_channel=AkshaySaini" target="_blank"><img src="https://img.youtube.com/vi/gSDncyuGw0s/0.jpg" width="750"
59-
alt="Functions and Variable Environments Youtube Link"/></a>
59+
alt="Functions and Variable Environments Youtube Link"/></a>

notes/season-1/lecture-05.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Episode 5 : Shortest JS Program, window & this keyword
22

3-
* The shortest JS program is empty file. Because even then, JS engine does a lot of things. As always, even in this case, it creates the GEC which has memory space and the execution context.
3+
- The shortest JS program is empty file. Because even then, JS engine does a lot of things. As always, even in this case, it creates the GEC which has memory space and the execution context.
44

5-
* JS engine creates something known as '**window**'. It is an object, which is created in the global space. It contains lots of functions and variables. These functions and variables can be accessed from anywhere in the program. JS engine also creates a **this** keyword, which points to the **window object** at the global level. So, in summary, along with GEC, a global object (window) and a this variable are created.
5+
- JS engine creates something known as '**window**'. It is an object, which is created in the global space. It contains lots of functions and variables. These functions and variables can be accessed from anywhere in the program. JS engine also creates a **this** keyword, which points to the **window object** at the global level. So, in summary, along with GEC, a global object (window) and a this variable are created.
66

7-
* In different engines, the name of global object changes. Window in browsers, but in nodeJS it is called something else. At global level, this === window
7+
- In different engines, the name of global object changes. Window in browsers, but in nodeJS it is called something else. At global level, this === window
88

9-
* If we create any variable in the global scope, then the variables get attached to the global object.
9+
- If we create any variable in the global scope, then the variables get attached to the global object.
1010

1111
eg:
12+
1213
```js
1314
var x = 10;
1415
console.log(x); // 10
@@ -21,4 +22,4 @@ console.log(window.x); // 10
2122
Watch Live On Youtube below:
2223

2324
<a href="https://www.youtube.com/watch?v=QCRpVw2KXf8&ab_channel=AkshaySaini" target="_blank"><img src="https://img.youtube.com/vi/QCRpVw2KXf8/0.jpg" width="750"
24-
alt="Shortest JS Program, window & this keyword Youtube Link"/></a>
25+
alt="Shortest JS Program, window & this keyword Youtube Link"/></a>

notes/season-1/lecture-06.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Episode 6 : undefined vs not defined in JS
22

3-
* In first phase (memory allocation) JS assigns each variable a placeholder called **undefined**.
3+
- In first phase (memory allocation) JS assigns each variable a placeholder called **undefined**.
44

5-
* **undefined** is when memory is allocated for the variable, but no value is assigned yet.
5+
- **undefined** is when memory is allocated for the variable, but no value is assigned yet.
66

7-
* If an object/variable is not even declared/found in memory allocation phase, and tried to access it then it is **Not defined**
7+
- If an object/variable is not even declared/found in memory allocation phase, and tried to access it then it is **Not defined**
88

9-
* Not Defined !== Undefined
9+
- Not Defined !== Undefined
1010

11-
> 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**.
11+
> 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**.
1212
1313
```js
1414
console.log(x); // undefined
@@ -17,12 +17,12 @@ console.log(x); // 25
1717
console.log(a); // Uncaught ReferenceError: a is not defined
1818
```
1919

20-
* 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 boolean *a = true* or string *a = 'hello'* later on.
21-
* **Never** assign *undefined* to a variable manually. Let it happen on it's own accord.
20+
- 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 boolean _a = true_ or string _a = 'hello'_ later on.
21+
- **Never** assign _undefined_ to a variable manually. Let it happen on it's own accord.
2222

2323
<hr>
2424

2525
Watch Live On Youtube below:
2626

2727
<a href="https://www.youtube.com/watch?v=B7iF6G3EyIk&ab_channel=AkshaySaini" target="_blank"><img src="https://img.youtube.com/vi/B7iF6G3EyIk/0.jpg" width="750"
28-
alt="undefined vs not defined in JS Youtube Link"/></a>
28+
alt="undefined vs not defined in JS Youtube Link"/></a>

notes/season-1/lecture-07.md

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Episode 7 : The Scope Chain, Scope & Lexical Environment
22

3-
* **Scope** in Javascript is directly related to **Lexical Environment**.
3+
- **Scope** in Javascript is directly related to **Lexical Environment**.
4+
5+
- Let's observe the below examples:
46

5-
* Let's observe the below examples:
67
```js
78
// CASE 1
89
function a() {
9-
console.log(b); // 10
10-
// Instead of printing undefined it prints 10, So somehow this a function could access the variable b outside the function scope.
10+
console.log(b); // 10
11+
// Instead of printing undefined it prints 10, So somehow this a function could access the variable b outside the function scope.
1112
}
1213
var b = 10;
1314
a();
@@ -16,10 +17,10 @@ a();
1617
```js
1718
// CASE 2
1819
function a() {
19-
c();
20-
function c() {
21-
console.log(b); // 10
22-
}
20+
c();
21+
function c() {
22+
console.log(b); // 10
23+
}
2324
}
2425
var b = 10;
2526
a();
@@ -28,11 +29,11 @@ a();
2829
```js
2930
// CASE 3
3031
function a() {
31-
c();
32-
function c() {
33-
var b = 100;
34-
console.log(b); // 100
35-
}
32+
c();
33+
function c() {
34+
var b = 100;
35+
console.log(b); // 100
36+
}
3637
}
3738
var b = 10;
3839
a();
@@ -41,21 +42,21 @@ a();
4142
```js
4243
// CASE 4
4344
function a() {
44-
var b = 10;
45-
c();
46-
function c() {
47-
console.log(b); // 10
48-
}
45+
var b = 10;
46+
c();
47+
function c() {
48+
console.log(b); // 10
49+
}
4950
}
5051
a();
5152
console.log(b); // Error, Not Defined
5253
```
5354

54-
* Let's try to understand the output in each of the cases above.
55-
* In **case 1**: function a is able to access variable b from Global scope.
56-
* In **case 2**: 10 is printed. It means that within nested function too, the global scope variable can be accessed.
57-
* In **case 3**: 100 is printed meaning local variable of the same name took precedence over a global variable.
58-
* In **case 4**: A function can access a global variable, but the global execution context can't access any local variable.
55+
- Let's try to understand the output in each of the cases above.
56+
- In **case 1**: function a is able to access variable b from Global scope.
57+
- In **case 2**: 10 is printed. It means that within nested function too, the global scope variable can be accessed.
58+
- In **case 3**: 100 is printed meaning local variable of the same name took precedence over a global variable.
59+
- In **case 4**: A function can access a global variable, but the global execution context can't access any local variable.
5960
```
6061
To summarize the above points in terms of execution context:
6162
call_stack = [GEC, a(), c()]
@@ -69,40 +70,39 @@ console.log(b); // Error, Not Defined
6970
7071
<br>
7172
72-
* So, **Lexical Environment** = local memory + lexical env of its parent. Hence, Lexical Environement is the local memory along with the lexical environment of its parent
73+
- So, **Lexical Environment** = local memory + lexical env of its parent. Hence, Lexical Environement is the local memory along with the lexical environment of its parent
7374
74-
* **Lexical**: In hierarchy, In order
75+
- **Lexical**: In hierarchy, In order
7576
76-
* Whenever an Execution Context is created, a Lexical environment(LE) is also created and is referenced in the local Execution Context(in memory space).
77+
- Whenever an Execution Context is created, a Lexical environment(LE) is also created and is referenced in the local Execution Context(in memory space).
7778
78-
* The process of going one by one to parent and checking for values is called scope chain or Lexcial environment chain.
79+
- The process of going one by one to parent and checking for values is called scope chain or Lexcial environment chain.
7980
80-
* ```js
81+
- ```js
8182
function a() {
82-
function c() {
83-
// logic here
84-
}
85-
c(); // c is lexically inside a
83+
function c() {
84+
// logic here
85+
}
86+
c(); // c is lexically inside a
8687
} // a is lexically inside global execution
8788
```
8889

89-
* Lexical or Static scope refers to the accessibility of variables, functions and object based on physical location in source code.
90-
```js
91-
Global {
92-
Outer {
93-
Inner
94-
}
95-
}
96-
// Inner is surrounded by lexical scope of Outer
97-
```
98-
90+
- Lexical or Static scope refers to the accessibility of variables, functions and object based on physical location in source code.
9991

100-
* **TLDR**; An inner function can access variables which are in outer functions even if inner function is nested deep. In any other case, a function can't access variables not in its scope.
92+
```js
93+
Global {
94+
Outer {
95+
Inner
96+
}
97+
}
98+
// Inner is surrounded by lexical scope of Outer
99+
```
101100

101+
- **TLDR**; An inner function can access variables which are in outer functions even if inner function is nested deep. In any other case, a function can't access variables not in its scope.
102102

103103
<hr>
104104

105105
Watch Live On Youtube below:
106106

107107
<a href="https://www.youtube.com/watch?v=uH-tVP8MUs8&ab_channel=AkshaySaini" target="_blank"><img src="https://img.youtube.com/vi/uH-tVP8MUs8/0.jpg" width="750"
108-
alt="The Scope Chain, Scope & Lexical Environment Youtube Link"/></a>
108+
alt="The Scope Chain, Scope & Lexical Environment Youtube Link"/></a>

0 commit comments

Comments
 (0)