Skip to content

Commit 2fd90d1

Browse files
updated to 2019 edition
1 parent c3f88b4 commit 2fd90d1

29 files changed

+574
-171
lines changed

README.md

+39-9
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,60 @@
1111
[![HitCount](http://hits.dwyl.io/albertomontalesi/JavaScript-es6-and-beyond-ebook.svg)](http://hits.dwyl.io/albertomontalesi/JavaScript-es6-and-beyond-ebook)
1212

1313

14-
# [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)
1515

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
1717

18-
![book-cover](/assets/Cover.jpg)
18+
![book-cover](/assets/Cover.png)
1919

20-
## Disclaimer
20+
## Who is this book for ?
2121

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.
2323

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.
2527

2628
## Where to buy it
2729

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)
2931

3032
You can also read my articles on my blog [here](https://www.inspiredwebdev.com/).
3133

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.
3335
You can have a look at the quizzes from the first two chapters in the `quizzes` folder.
3436

3537
## About me
3638

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).
3868

3969

4070
## License

assets/Cover.jpg

-67.4 KB
Binary file not shown.

assets/Cover.png

428 KB
Loading

assets/banner.png

170 KB
Loading

ebook/00_introduction.md

-29
This file was deleted.

ebook/01_var_let_const.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Chapter 1: Var vs Let vs Const & the temporal dead zone
22

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.
44

55
 
66

77
## `Var`
88

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.
1010

11-
``` javascript
11+
``` javascript
1212
for (var i = 0; i < 10; i++) {
1313
var leak = "I am available outside of the loop";
1414
}
@@ -26,13 +26,13 @@ console.log(functionScoped);
2626
// ReferenceError: functionScoped is not defined
2727
```
2828

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.
3030

3131
&nbsp;
3232

3333
## `Let`
3434

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.
3636

3737
``` javascript
3838
// using `let`
@@ -62,7 +62,7 @@ console.log(y);
6262
// expected output: block-scoped
6363
```
6464

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.
6666

6767
&nbsp;
6868

@@ -78,8 +78,7 @@ constant = " I can't be reassigned";
7878
// Uncaught TypeError: Assignment to constant variable
7979
```
8080

81-
82-
**Important**
81+
**Important**
8382
This **does not** mean that **const are immutable**.
8483

8584
&nbsp;
@@ -95,18 +94,19 @@ const person = {
9594
person.age = 26;
9695
console.log(person.age);
9796
// 26
98-
// in this case no error will be raised, we are not re-assigning the variable but just one of its properties.
9997
```
10098

99+
In this case we are not reassigning the whole variable but just one of its properties, which is perfectly fine.
100+
101101
---
102102

103103
&nbsp;
104104

105105
## The temporal dead zone
106106

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.
108108

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:
110110

111111
Let's look at an example:
112112

@@ -125,8 +125,9 @@ let j = "I am a let";
125125
`var` can be accessed **before** they are defined, but we can't access their **value**.
126126
`let` and `const` can't be accessed **before we define them**.
127127

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).
129129

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.
130131

131132
---
132133
&nbsp;
@@ -137,7 +138,6 @@ There is no rule stating where to use each of them and people have different opi
137138

138139
The first opinion comes from [Mathias Bynes:](https://mathiasbynens.be/notes/es6-const)
139140

140-
141141
- use `const` by default
142142
- use `let` only if rebinding is needed.
143143
- `var` should never be used in ES6.
@@ -150,3 +150,5 @@ The second opinion comes from [Kyle Simpson:]( blog.getify.com/constantly-confus
150150
- 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.
151151

152152
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.

ebook/02_arrow_functions.md

+70-20
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,43 @@ const greeting = () => {
3939

4040
## Implicitly return
4141

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:
4343

4444
``` javascript
4545
const greeting = name => `hello ${name}`;
4646
```
4747

48+
Look at a side by side comparison with an old ES5 Function:
49+
50+
```js
51+
const oldFunction = function(name){
52+
return `hello ${name}`
53+
}
54+
55+
const arrowFunction = 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+
const arrowFunction = (name) => {
63+
return `hello ${name}`;
64+
}
65+
```
66+
4867
Let's say we want to implicitly return an **object literal**, we would do like this:
4968

5069
``` javascript
5170
const race = "100m dash";
5271
const runners = [ "Usain Bolt", "Justin Gatlin", "Asafa Powell" ];
5372

54-
const winner = runners.map((runner, i) => ({ name: runner, race, place: i + 1}));
73+
const results = runners.map((runner, i) => ({ name: runner, race, place: i + 1}));
5574

56-
console.log(winner);
57-
// {name: "Usain Bolt", race: "100m dash", place: 1}
75+
console.log(results);
76+
// [{name: "Usain Bolt", race: "100m dash", place: 1}
5877
// {name: "Justin Gatlin", race: "100m dash", place: 2}
59-
// {name: "Asafa Powell", race: "100m dash", place: 3}
78+
// {name: "Asafa Powell", race: "100m dash", place: 3}]
6079
```
6180

6281
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.
@@ -77,7 +96,6 @@ const greeting = name => `hello ${name}`;
7796
greeting("Tom");
7897
```
7998

80-
8199
&nbsp;
82100

83101
## Arrow function and the `this` keyword
@@ -133,7 +151,7 @@ Here, the second `this` will inherit from its parent, and will be set to the `co
133151

134152
Using what we know about the inheritance of the `this` keyword we can define some instances where you should **not** use arrow functions.
135153

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.
137155

138156
``` javascript
139157
const button = document.querySelector("btn");
@@ -153,15 +171,33 @@ const person = {
153171
}
154172
```
155173

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+
function example(){
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:
157193

158194
```javascript
159-
const orderRunners = () => {
160-
const runners = Array.from(arguments);
161-
return runners.map((runner, i) => {
162-
return `#{runner} was number #{i +1}`;
163-
})
195+
const showWinner = () => {
196+
const winner = arguments[0];
197+
return `${winner} was the winner`
164198
}
199+
200+
showWinner( "Usain Bolt", "Justin Gatlin", "Asafa Powell" )
165201
```
166202

167203
This code will return:
@@ -170,14 +206,28 @@ This code will return:
170206
ReferenceError: arguments is not defined
171207
```
172208

173-
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.
212+
213+
Example with **arrow function**:
174214

175215
```javascript
176-
const orderRunners = (...args) => {
177-
const runners = Array.from(args);
178-
console.log(runners);
179-
return runners.map((runner, i) => {
180-
return `#{runner} was number #{i +1}`;
181-
})
216+
const showWinner = (...args) => {
217+
const winner = args[0];
218+
return `${winner} was the winner`
219+
}
220+
showWinner("Usain Bolt", "Justin Gatlin", "Asafa Powell" )
221+
// "Usain Bolt was the winner"
222+
```
223+
224+
Example with **function**:
225+
226+
```js
227+
const showWinner = function() {
228+
const winner = arguments[0];
229+
return `${winner} was the winner`
182230
}
231+
showWinner("Usain Bolt", "Justin Gatlin", "Asafa Powell")
232+
// "Usain Bolt was the winner"
183233
```

0 commit comments

Comments
 (0)