Skip to content

Commit 153ac2f

Browse files
add beyond es6 chapters 17 to 20
1 parent fe969a3 commit 153ac2f

File tree

5 files changed

+580
-3
lines changed

5 files changed

+580
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# JavaScript ES6 for beginners
1+
# JavaScript ES6 and beyond: Discover all the new features introduced to JavaScript from 2015 to 2018
22

3-
![](.gitbook/assets/cover.png)
3+
![book-cover](.gitbook/assets/cover.png)
44

55
## Disclaimer
66

@@ -14,7 +14,7 @@ My name is Alberto, I'm from Italy and I love programming. As I was studying ES6
1414

1515
Any contributions you make are of course greatly appreciated.
1616

17-
If you enjoy my content and you want to donate me a cup of coffee, you can do [here](https://github.com/AlbertoMontalesi/JavaScript-ES6-for-beginners-ebook/tree/33fc6a922b67c3f7e105bd14b3828b77a67ebdb4/paypal.me/albertomontalesi/README.md).
17+
If you enjoy my content and you want to donate me a cup of coffee, you can do so [here](https://github.com/AlbertoMontalesi/JavaScript-ES6-for-beginners-ebook/tree/33fc6a922b67c3f7e105bd14b3828b77a67ebdb4/paypal.me/albertomontalesi/README.md).
1818

1919
## License
2020

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Chapter 17: Everything new in ES7 (ES2016)
2+
3+
4+
5+
ES7 (or ES 2016) introduced only two new features :
6+
7+
- `Array.prototype.includes()`
8+
- the exponential operator
9+
10+
 
11+
12+
## `Array.prototype.includes()`
13+
14+
The `includes()` method will return `true` if our array includes a certain element, or `false` if it doesn't.
15+
16+
```js
17+
let array = [1,2,4,5];
18+
19+
array.includes(2);
20+
// true
21+
array.includes(3);
22+
// false
23+
```
24+
 
25+
26+
### Combine `includes()` with `fromIndex`
27+
28+
We can provide `.includes()` with an index where to begin searching for an element. Default is 0, but we can also pass a negative value.
29+
30+
The first value we pass is the element to search and the second one is the index:
31+
32+
``` js
33+
let array = [1,3,5,7,9,11];
34+
35+
array.includes(3,1);
36+
// true
37+
array.includes(5,4);
38+
//false
39+
array.includes(1,-1);
40+
// false
41+
array.includes(11,-3);
42+
// true
43+
```
44+
45+
`array.includes(5,4);` returned `false` because, despite the array actually contains the number 5, it is at the index 2 but we started looking at position 4. That's why we couldn't find it and it returned `false`.
46+
47+
`array.includes(1,-1);` returned `false` because we started looking at the index -1 (which is the last element of the array) and then continued from that point onwards.
48+
49+
`array.includes(11,-3);` returned `true` because we went back to the index -3 and moved up, finding the value 11 on our path.
50+
51+
 
52+
53+
## The exponential operator
54+
55+
Prior to ES7 we would have done this:
56+
57+
``` js
58+
Math.pow(2,2);
59+
// 4
60+
Math.pow(2,3);
61+
// 8
62+
```
63+
64+
Now with the new exponential operator we can do this:
65+
66+
```js
67+
2**2;
68+
// 4
69+
2**3;
70+
// 4
71+
```
72+
73+
It will get pretty useful when combining multiple operations like in this example:
74+
75+
``` js
76+
2**2**2;
77+
// 16
78+
Math.pow(Math.pow(2,2),2);
79+
// 16
80+
```
81+
82+
Using `Math.pow()` you need to continuously concatenate them and it can get pretty long and messy. The exponential operator provides a faster and cleaner way of doing the same thing.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Chapter 18: ES8 string padding, `Object.entries()`, `Object.values()` and more
2+
3+
ES8 (ES2017) introduced many new cool features, which we are going to see here. I will discuss `Async` and `Await` later as they deserve more attention.
4+
5+
## String padding(`padStart` and `padEnd`)
6+
7+
We can now add some padding to our strings, either at the end (`padEnd`) or at the beginning (`padStart`).
8+
9+
```js
10+
"hello".padStart(6);
11+
// " hello"
12+
"hello".padEnd(6);
13+
// "hello "
14+
```
15+
16+
We said we want 6 as our padding, but why in both cases we got only 1 space?
17+
It happens because `padStart` and `padEnd` will go and fill the empty spaces. In our example "hello" is 5 letters, and our padding is 6, which leaves only 1 empty space.
18+
19+
Look at this example
20+
21+
```js
22+
"hi".padStart(10);
23+
// 10 - 2 = 8 empty spaces
24+
// " hi"
25+
"welcome".padStart(10);
26+
// 10 - 6 = 4 empty spaces
27+
// " welcome"
28+
```
29+
30+
 
31+
32+
### Right align with `padStart`
33+
34+
We can use `padStart` if we want to right align something.
35+
36+
```js
37+
const strings = ["short", "medium length", "very long string"];
38+
39+
const longestString = strings.sort(str => str.length).map(str => str.length)[0];
40+
41+
strings.forEach(str => console.log(str.padStart(longestString)));
42+
43+
// very long string
44+
// medium length
45+
// short
46+
```
47+
48+
First we grabbed the longest of our strings and measured its length. We then applied a `padStart` to all the strings based on the length of the longest so that we now have all of them perfectly aligned to the right.
49+
50+
 
51+
52+
### Add a custom value to the padding
53+
54+
We are not bound to just add a white space as a padding, we can pass both strings and numbers.
55+
56+
```js
57+
"hello".padEnd(13," Alberto");
58+
// "hello Alberto"
59+
"1".padStart(3,0);
60+
// "001"
61+
"99".padStart(3,0);
62+
// "099"
63+
```
64+
65+
 
66+
67+
## `Object.entries()` and `Object.values()`
68+
69+
Let's first create an Object.
70+
71+
```js
72+
const family = {
73+
father: "Jonathan Kent",
74+
mother: "Martha Kent",
75+
son: "Clark Kent",
76+
}
77+
```
78+
79+
In previous versions of JavaScript we would have accessed the values inside the object like this:
80+
81+
```js
82+
Object.keys(family);
83+
// (3) ["father", "mother", "son"]
84+
family.father;
85+
"Jonathan Kent"
86+
```
87+
88+
`Object.keys()` returned us only the keys of the object that we then had to use to access the values.
89+
90+
We now have two more ways of accessing our objects:
91+
92+
```js
93+
Object.values(family);
94+
// (3) ["Jonathan Kent", "Martha Kent", "Clark Kent"]
95+
96+
Object.entries(family);
97+
// (2) ["father", "Jonathan Kent"]
98+
// (2) ["mother", "Martha Kent"]
99+
// (2) ["son", "Clark Kent"]
100+
```
101+
102+
`Object.values()` returns an array of all the values whilst `Object.entries()` returns an array of arrays containing both keys and values.
103+
104+
 
105+
106+
## `Object.getOwnPropertyDescriptors()`
107+
108+
This method will return all the own property descriptors of an object.
109+
The attributes it can return are `value`, `writable`, `get`, `set`, `configurable` and `enumerable`.
110+
111+
``` js
112+
const myObj = {
113+
name: "Alberto",
114+
age: 25,
115+
greet() {
116+
console.log("hello");
117+
},
118+
}
119+
Object.getOwnPropertyDescriptors(myObj);
120+
// age:{value: 25, writable: true, enumerable: true, configurable: true}
121+
122+
// greet:{value: ƒ, writable: true, enumerable: true, configurable: true}
123+
124+
// name:{value: "Alberto", writable: true, enumerable: true, configurable: true}
125+
```
126+
127+
 
128+
129+
## Trailing commas in function parameter lists and calls
130+
131+
This is just a minor change to a syntax. Now, when writing objects we need to leave a trailing comma after each parameter, whether or not it is the last one.
132+
133+
``` js
134+
// from this
135+
const object = {
136+
prop1: "prop",
137+
prop2: "propop"
138+
}
139+
140+
// to this
141+
const object = {
142+
prop1: "prop",
143+
prop2: "propop",
144+
}
145+
```
146+
147+
Notice how I wrote a comma at the end of the second property.
148+
It will not throw any error if you don't put it but it's a better practice to follow as it will make the life easier to your colleague or team members.
149+
150+
```js
151+
// I write
152+
const object = {
153+
prop1: "prop",
154+
prop2: "propop"
155+
}
156+
157+
// my colleague updates the code, adding a new property
158+
const object = {
159+
prop1: "prop",
160+
prop2: "propop"
161+
prop3: "propopop"
162+
}
163+
// suddenly he gets an error because he did not notice that I forgot to leave a comma at the end of the last parameter.
164+
```
165+
166+
167+
 
168+
169+
## Shared memory and `Atomics`
170+
171+
From [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics):
172+
173+
> When memory is shared, multiple threads can read and write the same data in memory. Atomic operations make sure that predictable values are written and read, that operations are finished before the next operation starts and that operations are not interrupted.
174+
175+
`Atomics` is not a constructor, all of its properties and methods are static (just like `Math`) therefore we cannot use it with a new operator or invoke the `Atomics` object as a function.
176+
177+
Examples of its methods are:
178+
179+
- add / sub
180+
- and / or / xor
181+
- load / store

0 commit comments

Comments
 (0)