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: 02_Code_Structure.md
+22-11Lines changed: 22 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# 🧱 JavaScript Code Structure
1
+
# CHAPTER 02: 🧱 JavaScript Code Structure
2
2
3
3
The building blocks of code.
4
4
@@ -15,7 +15,8 @@ alert("Hello, world!");
15
15
We can have as many statements in our code as we want. They can be separated with a semicolon (`;`):
16
16
17
17
```js
18
-
alert("Hello"); alert("World");
18
+
alert("Hello");
19
+
alert("World");
19
20
```
20
21
21
22
But usually, for readability, we write them on separate lines:
@@ -34,8 +35,8 @@ Semicolons can be **omitted** in many cases where a line break exists:
34
35
This would also work:
35
36
36
37
```js
37
-
alert("Hello")
38
-
alert("World")
38
+
alert("Hello");
39
+
alert("World");
39
40
```
40
41
41
42
Here, JavaScript interprets the line break as an **implicit** semicolon. This is called **automatic semicolon insertion**. But be careful—**not all cases** are safe.
@@ -45,9 +46,7 @@ Here, JavaScript interprets the line break as an **implicit** semicolon. This is
45
46
Example:
46
47
47
48
```js
48
-
alert(3+
49
-
1
50
-
+2);
49
+
alert(3+1+2);
51
50
```
52
51
53
52
✅ This works as expected and shows `6` because JavaScript does not insert semicolons here. JavaScript knows the line ends in `+`, so it continues.
@@ -71,12 +70,10 @@ Hello
71
70
But if you remove the semicolon after the `alert`:
72
71
73
72
```js
74
-
alert("Hello")
75
-
76
-
[(1, 2)].forEach(alert);
73
+
alert("Hello")[(1, 2)].forEach(alert);
77
74
```
78
75
79
-
It fails!
76
+
It fails!
80
77
The difference compared to the code above is only one character: the semicolon at the end of the first line is gone.
81
78
82
79
If we run this code, only the first Hello shows (and there’s an error, you may need to open the console to see it). There are no numbers any more.
### Everything in JS happens inside the **execution context.**
4
4
5
-
Assume execution context to be a big box/container where everything takes place(whole javascript code is executed).
5
+
Assume execution context to be a big box / container where everything takes place(whole javascript code is executed).
6
6
7
7
It has 2 components in it:
8
8
9
-
<li> <strong>Memory : </strong>The place where all the variables and functions are stored as (key:value) pairs. Memory component is also known as <em>variable environment</em>.
10
-
<li> <strong>Code : </strong>The place where code is executed one line at a time. Code component is also known as <em>Thread of Execution</em>
9
+
-**Memory:**The place where all the variables and functions are stored as (key:value) pairs. Memory component is also known as _variable environment_.
10
+
-**Code:**The place where code is executed one line at a time. Code component is also known as _Thread of Execution_
11
11
12
12
<br>
13
13
@@ -16,5 +16,19 @@ _Javascript is not possible without this beautiful execution context._
16
16
17
17
### JS is a **synchronous single-threaded language**.
18
18
19
-
<li> By single threaded, we mean JS can only run 1 command at a time
20
-
<li> By synchronous single threaded, we mean it can run 1 command at a time, <em>in a specific order</em>
19
+
- By single threaded, we mean JS can only run 1 command at a time
20
+
- By synchronous single threaded, we mean it can run 1 command at a time, **_in a specific order_**
21
+
22
+
---
23
+
24
+
<br><br>
25
+
26
+
<palign="left">
27
+
<ahref="./03_The_Modern_Mode_use_strict.md"><b>‹ GO TO PREVIOUS</b></a>
28
+
</p>
29
+
30
+
<palign="right">
31
+
<ahref="./05_Execution_Context_And_Call_Stack.md"><b>GO TO NEXT ›</b></a>
Copy file name to clipboardExpand all lines: 05_Execution_Context_And_Call_Stack.md
+17-3Lines changed: 17 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Episode 2 : Execution & Call Stack
1
+
# CHAPTER 05: Execution & Call Stack
2
2
3
3
**What happens when we run a js program?**
4
4
@@ -16,7 +16,7 @@ var square2 = square(n);
16
16
var square4 =square(4);
17
17
```
18
18
19
-
Now first, for this entire code a <strong>Global</strong> execution context is created.
19
+
Now first, for this entire code a **Global** execution context is created.
20
20
21
21
So, this execution context is created in 2 phases:
22
22
@@ -28,7 +28,7 @@ So, this execution context is created in 2 phases:
28
28
Here JS will allocate memory to all the variables and functions whenever they are encounterd while running whole js program line by line. In case of variables, it stores a special value called _undefined_, and in case of functions it literally stores the whole code of the function inside this memory space.
29
29
30
30
- Memory is allocated to variables and functions.
31
-
- For variable name(which is key) it assigns a value of <strong>undefined</strong>
31
+
- For variable name(which is key) it assigns a value of **undefined**
32
32
- For the function name(which is key) it assigns the entire function code as value.
33
33
34
34
```javascript
@@ -56,3 +56,17 @@ To manage all these EC, a **call stack** is created. Everytime code is run, the
56
56
> Call Stack maintains the order of execution of execution contexts
57
57
58
58
#### Call stack also known as Execution control stack, program stack, control stack, runtime stack and machine stack
59
+
60
+
---
61
+
62
+
<br><br>
63
+
64
+
<palign="left">
65
+
<ahref="./04_Execution_Context.md"><b>‹ GO TO PREVIOUS</b></a>
Copy file name to clipboardExpand all lines: 06_Hoisting.md
+20-2Lines changed: 20 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Episode 3 : Hoisting
1
+
# CHAPTER 06: Hoisting
2
2
3
3
## Code Example 01
4
4
@@ -60,7 +60,11 @@ Output:
60
60
- Not defined: We have not initialised the value for variable anywhere in the entire code and in memory space.
61
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
62
63
-
**Hoisting** is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting _error_
63
+
---
64
+
65
+
**`Hoisting`** is a concept which enables us to extract values of variables and functions even before initialising/assigning value without getting _error_
66
+
67
+
---
64
68
65
69
<br>
66
70
@@ -134,3 +138,17 @@ Output:
134
138
- 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
139
136
140
- This is the reason why we are getting these outputs.
141
+
142
+
---
143
+
144
+
<br><br>
145
+
146
+
<palign="left">
147
+
<ahref="./05_Execution_Context_And_Call_Stack.md"><b>‹ GO TO PREVIOUS</b></a>
148
+
</p>
149
+
150
+
<palign="right">
151
+
<ahref="./07_Functions_And_Variable_Environment.md"><b>GO TO NEXT ›</b></a>
Copy file name to clipboardExpand all lines: 07_Functions_And_Variable_Environment.md
+23-1Lines changed: 23 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Episode 4 : Functions and Variable Environments
1
+
# CHAPTER 07: Functions and Variable Environments
2
2
3
3
```javascript
4
4
var x =1;
@@ -31,12 +31,34 @@ Outputs:
31
31
### Code Flow
32
32
33
33
- The Global Execution Context (GEC) is created (the big box with Memory and Code subparts). Also GEC is pushed into Call Stack
34
+
34
35
> Call Stack : GEC
36
+
35
37
- In first phase of GEC (memory phase), variable `x:undefined` and `a` and `b` have their entire function code as value initialized
36
38
- 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.
39
+
37
40
> Call Stack: [GEC, a()]
41
+
38
42
- 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
43
+
39
44
> Call Stack: GEC
45
+
40
46
- Cursor goes back to `b()` function call. Same steps repeat.
47
+
41
48
> Call Stack :[GEC, b()] -> GEC (after printing yet another totally different x value as 100 in console log)
49
+
42
50
- Finally GEC is deleted and also removed from call stack. Program ends.
51
+
52
+
---
53
+
54
+
<br><br>
55
+
56
+
<palign="left">
57
+
<ahref="./06_Hoisting.md"><b>‹ GO TO PREVIOUS</b></a>
58
+
</p>
59
+
60
+
<palign="right">
61
+
<ahref="./08_window_And_this.md"><b>GO TO NEXT ›</b></a>
Copy file name to clipboardExpand all lines: 08_window_And_this.md
+17-2Lines changed: 17 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Episode 5: Window and this keyword
1
+
# CHAPTER 08: Window and this keyword
2
2
3
3
### Everywhere JS is run, it is done with a JS execution engine. For Chrome: v8
4
4
@@ -7,6 +7,7 @@
7
7
- It creates the GEC, the "window" and the _this_ variable.
8
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
9
- At the global level, _this_ points to _window_ object.
10
+
10
11
> this === window -> true (at global level)
11
12
12
13
_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._
@@ -28,5 +29,19 @@ console.log(a); //also gives same "a" value. (if we don't put any . in front of
28
29
console.log(x); // x is not defined. (tries to find x inside global space, but it isn't there)
29
30
```
30
31
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
+
- 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
33
- 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.
34
+
35
+
---
36
+
37
+
<br><br>
38
+
39
+
<palign="left">
40
+
<ahref="./07_Functions_And_Variable_Environment.md"><b>‹ GO TO PREVIOUS</b></a>
41
+
</p>
42
+
43
+
<palign="right">
44
+
<ahref="./09_undefined_vs_not_defined.md"><b>GO TO NEXT ›</b></a>
Copy file name to clipboardExpand all lines: 09_undefined_vs_not_defined.md
+16-2Lines changed: 16 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Episode 6: Undefined vs Not Defined
1
+
# CHAPTER 09: Undefined vs Not Defined
2
2
3
3
- In first phase (memory allocation) JS assigns each variable to a placeholder called _undefined_
4
4
-_undefined_ is when memory is allocated for the variable, but no value is assigned yet.
@@ -20,5 +20,19 @@ console.log(a);
20
20
25
21
21
Uncaught ReferenceError: a is not defined
22
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.
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
24
-**Never** assign _undefined_ to a variable manually. Let it happen on it's own accord.
25
+
26
+
---
27
+
28
+
<br><br>
29
+
30
+
<palign="left">
31
+
<ahref="./08_window_And_this.md"><b>‹ GO TO PREVIOUS</b></a>
32
+
</p>
33
+
34
+
<palign="right">
35
+
<ahref="./10_Scope_And_Lexical_Environment.md"><b>GO TO NEXT ›</b></a>
0 commit comments