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
# [The Complete Guide to Modern JavaScript ](https://www.amazon.com/dp/B07DGGFNS6)
14
+
# [The Complete Guide to Modern JavaScript ](https://www.amazon.com/dp/B07S2M3FVV)
15
15
16
-
## Discover all the new features introduced to JavaScript from 2015 to 2018
16
+
## Discover all the new features introduced to JavaScript from 2015 to 2019
17
17
18
-

18
+

19
19
20
-
## Disclaimer
20
+
## Who is this book for ?
21
21
22
-
This book is intended for somebody already familiar with the basics of JavaScript, as I am only focusing on the new features introduced by ES6 and I won't be explaining what is a `var`, how to create a function, etc...
22
+
Whether you are a complete beginner or you have some knowledge in JavaScript, this book will guide you from the basics of the language to all the new features introduced until 2019. At the end of each chapter test your knowledge with quizzes.
23
23
24
-
Additional chapters cover the new features introduced post ES6 (ES2015) all the way to the most recent version, ES2018.
24
+
After reading this book, Let Const, generators, promises and async won't be a problem anymore.
25
+
26
+
If you want to experience something new, this book also includes an introduction to the basics of TypeScript, a must-know for any JavaScript develop in 2019.
25
27
26
28
## Where to buy it
27
29
28
-
You can purchase this ebook on amazon.com at this [link](https://www.amazon.com/dp/B07DGGFNS6) or click [here](http://bit.ly/2kGMwKn) if you are not in the US to find the right amazon store.
30
+
You can purchase this ebook on amazon.com at this [link](https://www.amazon.com/dp/B07S2M3FVV)
29
31
30
32
You can also read my articles on my blog [here](https://www.inspiredwebdev.com/).
31
33
32
-
The ebook contains 50+ extra quizzes for your to practice and test your knowledge after reading each chapter.
34
+
The ebook includes two sections dedicated to the basics of JavaScript and TypeScript and also contains 50+ extra quizzes for your to practice and test your knowledge after reading each chapter.
33
35
You can have a look at the quizzes from the first two chapters in the `quizzes` folder.
34
36
35
37
## About me
36
38
37
-
My name is Alberto, I'm from Italy and I love programming. As I was studying ES6 I decided that the best way for me to test my understanding of it was to write articles about it. I have now packaged those articles in this ebook.
39
+
My name is Alberto Montalesi, I am from Italy and I am working in Vietnam as a Software Developer creating enterprise software.
40
+
41
+
My passion for programming started late in life, in 2016 at the age of 24 after a bachelor's degree in Law.
42
+
43
+
My path to become a self-taught software developer has not been easy, but it's definitely something I would do again.
44
+
45
+
You can read my story on Medium at this link: https://medium.freecodecamp.org/my-journey-from-esl-teacher-to-software-developer-35cc998a6ec0
46
+
47
+
Writing a book that can help other aspiring developers fills me with pride as I know very well how hard it can be to find the motivation and the resources to continue studying and improving your skill.
48
+
49
+
Apart from programming, my other passions include photography, traveling and gaming.
50
+
51
+
52
+
53
+
## Get in touch
54
+
55
+
If you want to get in touch for any type of collaboration or discussion you can find me on:
56
+
57
+
-[Medium](https://medium.com/@labby92) at https://medium.com/@labby92
58
+
-[Github](https://github.com/AlbertoMontalesi) at https://github.com/AlbertoMontalesi
59
+
-[InspiredWebDev](https://inspiredwebdev.com) my personal blog https://inspiredwebdev.com
60
+
61
+
62
+
63
+
## Contributions & Donations
64
+
65
+
Any contributions you make are of course greatly appreciated.
66
+
67
+
If you enjoy my content and you want to donate a cup of coffee to me, you can do so at [https://www.paypal.me/albertomontalesi](https://www.paypal.me/albertomontalesi).
Copy file name to clipboardExpand all lines: ebook/01_var_let_const.md
+15-13
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Chapter 1: Var vs Let vs Const & the temporal dead zone
2
2
3
-
With the introduction of `let` and `const` in **ES6**, we can now better define our variables depending on our needs. Let's have a look at the major differences between them.
3
+
With the introduction of `let` and `const` in **ES6**, we can now better define our variables depending on our needs. During our JavaScript primer we looked at the basic differences between these 3 keywords, now we will go into more detail.
4
4
5
5
6
6
7
7
## `Var`
8
8
9
-
`var` are **function scoped**, which means that if we declare them inside a `for` loop (which is a **block** scope) they will be available even outside of it.
9
+
Variables declared with the `var` keyword are **function scoped**, which means that if we declare them inside a `for` loop (which is a **block** scope) they will be available even outside of it.
10
10
11
-
```javascript
11
+
```javascript
12
12
for (var i =0; i <10; i++) {
13
13
var leak ="I am available outside of the loop";
14
14
}
@@ -26,13 +26,13 @@ console.log(functionScoped);
26
26
// ReferenceError: functionScoped is not defined
27
27
```
28
28
29
-
In the first example the value of the `var`leaked out of the block-scope and could be accessed from outside of it, whereas in the second example `var` was confined inside a function-scope and we could not access it from outside.
29
+
In the first example the value of the `var` leaked out of the block-scope and could be accessed from outside of it, whereas in the second example `var` was confined inside a function-scope and we could not access it from outside.
30
30
31
31
32
32
33
33
## `Let`
34
34
35
-
`let` (and `const`) are **block scoped**, meaning that they will be available only inside of the block where they are declared and its sub-blocks.
35
+
Variables declared with the `let` (and `const`) keyword are **block scoped**, meaning that they will be available only inside of the block where they are declared and its sub-blocks.
36
36
37
37
```javascript
38
38
// using `let`
@@ -62,7 +62,7 @@ console.log(y);
62
62
// expected output: block-scoped
63
63
```
64
64
65
-
As you can see, when we assigned a new value to our `let` inside our block-scope, it **did not** change its value in the global scope, whereas when did the same with our `var` it leaked outside of the block-scope and also changed it in the global scope.
65
+
As you can see, when we assigned a new value to our `let` inside our block-scope, it **did not** change its value in the outer scope, whereas when did the same with our `var` it leaked outside of the block-scope and also changed it in the outer scope.
66
66
67
67
68
68
@@ -78,8 +78,7 @@ constant = " I can't be reassigned";
78
78
// Uncaught TypeError: Assignment to constant variable
79
79
```
80
80
81
-
82
-
**Important**
81
+
**Important**
83
82
This **does not** mean that **const are immutable**.
84
83
85
84
@@ -95,18 +94,19 @@ const person = {
95
94
person.age=26;
96
95
console.log(person.age);
97
96
// 26
98
-
// in this case no error will be raised, we are not re-assigning the variable but just one of its properties.
99
97
```
100
98
99
+
In this case we are not reassigning the whole variable but just one of its properties, which is perfectly fine.
100
+
101
101
---
102
102
103
103
104
104
105
105
## The temporal dead zone
106
106
107
-
According to **MDN**:
107
+
Now we will have a look at a very important concept which may sound complicate from its name, but i reassure you it is not.
108
108
109
-
> In ECMAScript 2015, let bindings are not subject to **Variable Hoisting**, which means that let declarations do not move to the top of the current execution context. Referencing the variable in the block before the initialization results in a ReferenceError (contrary to a variable declared with var, which will just have the undefined value). The variable is in a “temporal dead zone” from the start of the block until the initialization is processed.
109
+
First let's have a look at a simple example:
110
110
111
111
Let's look at an example:
112
112
@@ -125,8 +125,9 @@ let j = "I am a let";
125
125
`var` can be accessed **before** they are defined, but we can't access their **value**.
126
126
`let` and `const` can't be accessed **before we define them**.
127
127
128
-
This happens because `var` are subject to **hoisting**, which means that they are processed before any code is executed. Declaring a `var` anywhere is equivalent to **declaring it at the top**. This is why we can still access the `var` but we can't yet see its content, hence the `undefined` result.
128
+
Despite what you may read on other sources, both `var`and `let`(and `const`) are subject to **hoisting** which measn thath they are processe before any code is executed and lifted up to the top of their scope (whether it's global or block).
129
129
130
+
The main differences lays in the fact that `var` can still be accessed before they are defined, causing the value to be `undefined` while on the other hand, `let` variables sit in a **temporal dead zone** until they are declared, causing an error when accessed before initialization which makes it easier to debug code rather than having an `undefined` as the result.
130
131
131
132
---
132
133
@@ -137,7 +138,6 @@ There is no rule stating where to use each of them and people have different opi
137
138
138
139
The first opinion comes from [Mathias Bynes:](https://mathiasbynens.be/notes/es6-const)
139
140
140
-
141
141
- use `const` by default
142
142
- use `let` only if rebinding is needed.
143
143
-`var` should never be used in ES6.
@@ -150,3 +150,5 @@ The second opinion comes from [Kyle Simpson:]( blog.getify.com/constantly-confus
150
150
- Refactor `let` to `const` only after some code has to be written, and you're reasonably sure that you've got a case where there shouldn't be variable reassignment.
151
151
152
152
Which opinion to follow is entirely up to you. As always, do your own research and figure out which one you think is the best.
153
+
154
+
My personal opinion is to always use `const` by default and then switch to `let` if you see yourself in need of rebinding the value.
Copy file name to clipboardExpand all lines: ebook/02_arrow_functions.md
+70-20
Original file line number
Diff line number
Diff line change
@@ -39,24 +39,43 @@ const greeting = () => {
39
39
40
40
## Implicitly return
41
41
42
-
With arrow functions we can skip the explicit return and return like this:
42
+
With arrow functions we can skip the explicit `return` and return like this:
43
43
44
44
```javascript
45
45
constgreeting=name=>`hello ${name}`;
46
46
```
47
47
48
+
Look at a side by side comparison with an old ES5 Function:
49
+
50
+
```js
51
+
constoldFunction=function(name){
52
+
return`hello ${name}`
53
+
}
54
+
55
+
constarrowFunction=name=>`hello ${name}`;
56
+
```
57
+
58
+
Both functions achieve the same result, but the new syntax allows you to be more concise.
59
+
Beware! Readability is more important than conciceness so you might want to write your funciton like this if you are working in a team and not everybody is totally up-to-date with ES6.
60
+
61
+
```js
62
+
constarrowFunction= (name) => {
63
+
return`hello ${name}`;
64
+
}
65
+
```
66
+
48
67
Let's say we want to implicitly return an **object literal**, we would do like this:
To tell JavaScript that what's inside the curly braces is an **object literal** that we want to implicitly return, we need to wrap everything inside parenthesis.
@@ -133,7 +151,7 @@ Here, the second `this` will inherit from its parent, and will be set to the `co
133
151
134
152
Using what we know about the inheritance of the `this` keyword we can define some instances where you should **not** use arrow functions.
135
153
136
-
The next 2 examples all show when to be careful using `this` inside of arrows.
154
+
The next 2 examples show when to be careful using `this` inside of arrows.
137
155
138
156
```javascript
139
157
constbutton=document.querySelector("btn");
@@ -153,15 +171,33 @@ const person = {
153
171
}
154
172
```
155
173
156
-
One other difference between Arrow functions and normal functions is access to the `arguments`.
174
+
Another difference between Arrow functions and normal functions is the access to the `arguments object`.
175
+
The `arguments object` is an array-like object that we can access from inside functions and contains the values of the arguments passed to that function.
176
+
177
+
A quick example:
178
+
179
+
```js
180
+
functionexample(){
181
+
console.log(arguments[0])
182
+
}
183
+
184
+
example(1,2,3);
185
+
// 1
186
+
```
187
+
188
+
As you can see we accessed the first argument using an array notation `arguments[0]`.
189
+
190
+
Similarly as what we saw with the `this` keyword, Arrow functions inherit the value of the `arguments object` from their parent scope.
191
+
192
+
Let's have a look at this example with our previous list of runners:
To access all the arguments of an array, use old function notation, or the splat syntax. The name of the parameter does not matter (here it is called args to reduce confusion with `arguments`).
209
+
To access all the arguments passed to the function we can either use the old function notation or the spread syntax(which we will discuss more in Chapter 9)
210
+
211
+
Remember that `arguments` it's just a keyword, it's not a variable name.
0 commit comments