Skip to content

Commit fa5a607

Browse files
4th edition - ES2021
1 parent 4eb72af commit fa5a607

File tree

6 files changed

+254
-15
lines changed

6 files changed

+254
-15
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/AlbertoMontalesi/JavaScript-es6-and-beyond-ebook/pulls)
1010

11-
# [The Complete Guide to Modern JavaScript ](https://amzn.to/2H76VUz)
11+
# [The Complete Guide to Modern JavaScript ](https://leanpub.com/completeguidetomodernjavascript2020)
1212

13-
## Learn everything from the basics of JavaScript to the new ES2020 features. Practice with more than 50 quizzes and dive into the basics of TypeScript.
13+
## Learn everything from the basics of JavaScript to the new ES2021 features. Practice with more than 50 quizzes and dive into the basics of TypeScript.
1414

1515
[![book-cover](/assets/banner.jpg)](http://bit.ly/2VV2LbX)
1616

@@ -22,6 +22,10 @@ This book is **not** for total beginners, it does **cover** the basics of progra
2222

2323
 
2424

25+
## 4th Edition is out
26+
27+
Included a new chapter detailing all the new features introduced by ES2021
28+
2529
## Free vs Paid Version
2630

2731
The Paid version gives you access to:
@@ -45,9 +49,8 @@ You can get the course based on this book on:
4549
You get the ebook on Amazon, Leanpub and other stores, check the following link to find your local amazon store:
4650

4751
- Play Store [Ebook](https://play.google.com/store/books/details/Alberto_Montalesi_The_Complete_Guide_to_Modern_Jav?id=avqrDwAAQBAJ)
48-
- Kobo [Ebook](https://www.kobo.com/ww/en/ebook/complete-guide-to-modern-javascript)
4952
- Leanpub: [Ebook](https://leanpub.com/completeguidetomodernjavascript2020)
50-
- Amazon Global: [Ebook](http://a-fwd.to/jHO6m9t) | [Paperback](http://a-fwd.to/3BiBhIM)
53+
- Amazon Global: [Paperback](http://a-fwd.to/3BiBhIM)
5154

5255
If you enjoyed the book please leave a review to support me and help other buyers.
5356

@@ -79,7 +82,7 @@ If you want to get in touch for any type of collaboration or discussion you can
7982
- [DevTo](https://dev.to/albertomontalesi) at https://dev.to/albertomontalesi
8083
- [Medium](https://medium.com/@labby92) at https://medium.com/@labby92
8184
- [Github](https://github.com/AlbertoMontalesi) at https://github.com/AlbertoMontalesi
82-
85+
8386
 
8487

8588
## Contributions & Donations

assets/cover.jpg

-492 KB
Binary file not shown.

assets/cover.png

1.7 MB
Loading

ebook/10_object_literal_upgrades.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const person = {
2323
surname: surname,
2424
age: age,
2525
nationality: nationality,
26-
}
26+
};
2727

2828
console.log(person);
2929
// { name: 'Alberto',
@@ -40,7 +40,7 @@ const person = {
4040
surname,
4141
age,
4242
nationality,
43-
}
43+
};
4444
console.log(person);
4545
// {name: "Alberto", surname: "Montalesi", age: 25, nationality: "Italian"}
4646
```
@@ -56,10 +56,10 @@ Let's look at an example from ES5:
5656
```javascript
5757
const person = {
5858
name: "Alberto",
59-
greet: function(){
59+
greet: function () {
6060
console.log("Hello");
6161
},
62-
}
62+
};
6363

6464
person.greet();
6565
// Hello
@@ -70,10 +70,10 @@ If we wanted to add a function to our object, we had to use the `function` keywo
7070
```javascript
7171
const person = {
7272
name: "Alberto",
73-
greet(){
73+
greet() {
7474
console.log("Hello");
7575
},
76-
}
76+
};
7777

7878
person.greet();
7979
// Hello;
@@ -105,10 +105,10 @@ This is how we would dynamically define properties of an object in ES5:
105105
```javascript
106106
var name = "myname";
107107
// create empty object
108-
var person = {}
108+
var person = {};
109109
// update the object
110110
person[name] = "Alberto";
111-
console.log(person.myname);
111+
console.log(person.name);
112112
// Alberto
113113
```
114114

@@ -117,8 +117,8 @@ In the example given above, first we created the object and then we modified it.
117117
```javascript
118118
const name = "myname";
119119
const person = {
120-
[name]:"Alberto",
120+
[name]: "Alberto",
121121
};
122-
console.log(person.myname);
122+
console.log(person.name);
123123
// Alberto
124124
```

ebook/23_what_new_es2021.md

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Chapter 23: What's new in ES2021
2+
3+
Let's get started with the first of the new ES2021 features:
4+
5+
## String.prototype.replaceAll
6+
7+
`String.replace` is a useful method that allows us to replace a pattern in a string with something else. The problem is that if we want to use a `string` as a pattern and not a RegEx, only the **first** occurrence will get replaced.
8+
9+
```js
10+
const str = "I like my dog, my dog loves me";
11+
const newStr = str.replace("dog", "cat");
12+
newStr;
13+
// "I like my cat, my dog loves me"
14+
```
15+
16+
As the name implies, `String.replaceAll` will do exactly what we need in this scenario, replace all the matching pattern, allowing us to easily replace all mentions of a substring, without the use of RegEx:
17+
18+
```js
19+
const str = "I like my dog, my dog loves me";
20+
const newStr = str.replaceAll("dog", "cat");
21+
newStr;
22+
// "I like my cat, my cat loves me"
23+
```
24+
25+
[Read More](https://github.com/tc39/proposal-string-replaceall)
26+
27+
 
28+
29+
## Promise.any
30+
31+
During the past years we've seen new methods such as `Promise.all` and `Promise.race` with ES6, `Promise.allSettled` last year with ES2020 and ES2021 will introduce a new one: `Promise.any`.
32+
33+
I bet you can already tell what it does from the name of the method.
34+
35+
`Promise.any` shorts-circuits once a given promise is fulfilled but will continue until all promises are rejected.
36+
37+
Don't get confused with `Promise.race` because with `race`, the promise short-circuits once one of the given promises resolves **or rejects**.
38+
39+
They have similar behavior for what concerns fulfillment but very different for rejection.
40+
41+
If all the promises inside of `Promise.any` fails, it will throw an `AggregateError` (a subclass of `Error`) containing the rejection reasons of all the promises.
42+
43+
We can use it like this:
44+
45+
```javascript
46+
// example taken from: https://github.com/tc39/proposal-promise-any
47+
Promise.any(promises).then(
48+
(first) => {
49+
// Any of the promises was fulfilled.
50+
},
51+
(error) => {
52+
// All of the promises were rejected.
53+
}
54+
);
55+
```
56+
57+
[Read More](https://github.com/tc39/proposal-promise-any)
58+
59+
 
60+
61+
## Logical Operators and Assignment Expressions
62+
63+
With `ES2021` we will be able to combine Logical Operators (`&&`, `||` and `??`) with Assignment Expression (`=`) similarly to how it's already possible to do in Ruby.
64+
65+
If you skipped on `ES2020` you may not be aware of `??` which is the **nullish coalescing** operator. Let's look at an example:
66+
67+
```js
68+
const a = null ?? "test";
69+
// 'test'
70+
const b = 0 ?? "test";
71+
// 0
72+
```
73+
74+
The **nullish coalescing** operator returns the _right_ hand-side when the left-hand-side is `null` or `undefined`, otherwise it returns the _left_ hand-side. In the first example the left-hand-side was `null` thus it returned the right-hand-side while on the second example it returned the left-hand-side because it was neither `null` nor `undefined`.
75+
76+
Moving back to ES2021 stuff, in `JavaScript` we already have many [assignment opeartors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#Assignment_operators) like the following basic example:
77+
78+
```js
79+
let a = 0;
80+
a += 2;
81+
// 2
82+
```
83+
84+
But with this new proposal we will be able to do the following:
85+
86+
```js
87+
a ||= b;
88+
// equivalent to a = a || b
89+
90+
c &&= d;
91+
// equivalent to c = c && d
92+
93+
e ??= f;
94+
// equivalent to e = e ?? f
95+
```
96+
97+
Let's go over each one by one:
98+
99+
- `a ||= b` will return `a` if `a` is a truthy value, or `b` if `a` is falsy
100+
- `c &&= d` will return `d` if both `c` and `d` are truthy, or `c` otherwise
101+
- `e ??= f` will return `f` if `e` is `null` or `undefined` otherwise it will return `e`
102+
103+
[Read More](https://github.com/tc39/proposal-logical-assignment)
104+
105+
 
106+
107+
## Numeric Separators
108+
109+
The introduction of Numeric Separators will make it easier to read numeric values by using the `_` (underscore) character to provide a separation between groups of digits.
110+
111+
Let's look at more examples:
112+
113+
```js
114+
x = 100_000;
115+
// 100 thousand
116+
117+
dollar = 55_90;
118+
// 55 dollar 90 cents
119+
120+
fraction = 0.000_1;
121+
// 1 thousandth
122+
```
123+
124+
[Read more](https://github.com/tc39/proposal-numeric-separator)
125+
126+
 
127+
128+
## WeakRefs
129+
130+
From [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef#:~:text=A%20weak%20reference%20to%20an%20object%20is%20a%20reference%20that,reclaimed%20by%20the%20garbage%20collector.&text=When%20an%20object%20no%20longer,object%20and%20reclaim%20its%20memory.): A weak reference to an object is a reference that does not prevent the object from being reclaimed by the garbage collector.
131+
132+
With this new proposal for ES2021, we will be able to create weak references to objects with the `WeakRef` class. Please follow the link below to read a more in-depth explanation.
133+
134+
[Read More](https://github.com/tc39/proposal-weakrefs)
135+
136+
 
137+
138+
## Intl.ListFormat
139+
140+
The `Intl.ListFormat` object is a constructor for objects that enable language-sensitive list formatting.
141+
142+
Looking at an example is easier than explaining it:
143+
144+
```js
145+
const list = ["Apple", "Orange", "Banana"];
146+
147+
new Intl.ListFormat("en-GB", { style: "long", type: "conjunction" }).format(
148+
list
149+
);
150+
// Apple, Orange and Banana
151+
152+
new Intl.ListFormat("en-GB", { style: "short", type: "disjunction" }).format(
153+
list
154+
);
155+
// Apple, Orange or Banana
156+
```
157+
158+
You are not limited to English, let's try with a few different languages:
159+
160+
```js
161+
const list = ["Apple", "Orange", "Banana"];
162+
163+
// Italian
164+
console.log(
165+
new Intl.ListFormat("it", { style: "long", type: "conjunction" }).format(list)
166+
);
167+
// Apple, Orange e Banana
168+
169+
// Spanish
170+
console.log(
171+
new Intl.ListFormat("es", { style: "long", type: "conjunction" }).format(list)
172+
);
173+
// Apple, Orange y Banana
174+
175+
// German
176+
console.log(
177+
new Intl.ListFormat("de", { style: "long", type: "conjunction" }).format(list)
178+
);
179+
// Apple, Orange und Banana
180+
```
181+
182+
Pretty neat uh? For a more detailed look at this specification check out the link below.
183+
184+
[Read More](https://github.com/tc39/proposal-intl-list-format)
185+
186+
 
187+
188+
## dateStyle and timeStyle options for Intl.DateTimeFormat
189+
190+
We can use `dateStyle` and `timeStyle` to request a locale-specific date and time of a given length.
191+
192+
```js
193+
// short
194+
new Intl.DateTimeFormat("en", {
195+
timeStyle: "short",
196+
}).format(Date.now());
197+
// "2:45 PM"
198+
199+
// medium
200+
new Intl.DateTimeFormat("en", {
201+
timeStyle: "medium",
202+
}).format(Date.now());
203+
// "2:45:53 PM"
204+
205+
// long
206+
new Intl.DateTimeFormat("en", {
207+
timeStyle: "long",
208+
}).format(Date.now());
209+
// "2:46:05 PM GMT+7"
210+
```
211+
212+
Now let's try with `dateStyle`:
213+
214+
```js
215+
// short
216+
new Intl.DateTimeFormat("en", {
217+
dateStyle: "short",
218+
}).format(Date.now());
219+
// "7/25/20"
220+
221+
// medium
222+
new Intl.DateTimeFormat("en", {
223+
dateStyle: "medium",
224+
}).format(Date.now());
225+
// "Jul 25, 2020"
226+
227+
// long
228+
new Intl.DateTimeFormat("en", {
229+
dateStyle: "long",
230+
}).format(Date.now());
231+
// "July 25, 2020"
232+
```
233+
234+
You can pass whatever locale you want and you can also pass both `dateStyle` and `timeStyle` options at the same time, choosing between the three options of 'short', 'medium', and 'long' that best suit your needs.
235+
236+
[Read More](https://github.com/tc39/proposal-intl-datetime-style)
File renamed without changes.

0 commit comments

Comments
 (0)