Skip to content

Commit a8f3034

Browse files
AlbertoMontalesigitbook-bot
authored andcommitted
GitBook: [master] 19 pages and one asset modified
1 parent 33fc6a9 commit a8f3034

19 files changed

+253
-413
lines changed

.gitbook/assets/cover.png

111 KB
Loading

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
# JavaScript ES6 for beginners
22

3-
<img src="assets/cover.png" width="300">
3+
![](.gitbook/assets/cover.png)
44

55
## Disclaimer
66

77
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...
88

99
## About me
1010

11-
My name is Alberto, I'm from Italy and I love programming.
12-
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 a free ebook that you can read here or on my blog [here](http://albertomontalesi.github.io/).
11+
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 a free ebook that you can read here or on my blog [here](http://albertomontalesi.github.io/).
1312

1413
## Contributions & Donations
1514

1615
Any contributions you make are of course greatly appreciated.
1716

18-
If you enjoy my content and you want to donate me a cup of coffee, you can do [here](paypal.me/albertomontalesi).
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).
1918

2019
## Table of contents
2120

@@ -36,7 +35,7 @@ If you enjoy my content and you want to donate me a cup of coffee, you can do [h
3635
* [Chapter 15: Proxies](https://github.com/AlbertoMontalesi/JavaScript-ES6-for-beginners-ebook/blob/master/ebook/15_proxies.md)
3736
* [Chapter 16: Sets, WeakSets, Maps and WeakMaps](https://github.com/AlbertoMontalesi/JavaScript-ES6-for-beginners-ebook/blob/master/ebook/16_sets_weaksets_maps_weakmaps.md)
3837

39-
4038
## License
4139

42-
This book is licensed under a [Creative Commons Attribution 4.0 - cc-by-4.0](https://creativecommons.org/licenses/by/4.0/)
40+
This book is licensed under a [Creative Commons Attribution 4.0 - cc-by-4.0](https://creativecommons.org/licenses/by/4.0/)
41+

SUMMARY.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Table of contents
2+
3+
* [JavaScript ES6 for beginners](README.md)
4+
* ebook
5+
* [Chapter 1: Var vs Let vs Const & the temporal dead zone](ebook/01_var_let_const.md)
6+
* [Chapter 2: Arrow functions](ebook/02_arrow_functions.md)
7+
* [Chapter 3: Default function arguments](ebook/03_default_function_arguments.md)
8+
* [Chapter 4: Template literals](ebook/04_template_literals.md)
9+
* [Chapter 5: Additional string methods](ebook/05_additional_string_methods.md)
10+
* [Chapter 6: Destructuring](ebook/06_destructuring.md)
11+
* [Chapter 7: Iterables and looping](ebook/07_iterables-and-looping.md)
12+
* [Chapter 8: Array improvements](ebook/08_array_improvements.md)
13+
* [Chapter 9: Spread operator and rest parameters](ebook/09_spread_operator_and_rest_parameters.md)
14+
* [Chapter 10: Object literal upgrades](ebook/10_object_literal_upgrades.md)
15+
* [Chapter 11: Symbols](ebook/11_symbols.md)
16+
* [Chapter 12: Classes](ebook/12_classes.md)
17+
* [Chapter 13: Promises](ebook/13_promises.md)
18+
* [Chapter 14: Generators](ebook/14_generators.md)
19+
* [Chapter 15: Proxies](ebook/15_proxies.md)
20+
* [Chapter 16: Sets, WeakSets, Maps and WeakMaps](ebook/16_sets_weaksets_maps_weakmaps.md)
21+

ebook/01_var_let_const.md

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
# Chapter 1: `Var` vs `Let` vs `Const` & the temporal dead zone
1+
# Chapter 1: Var vs Let vs Const & the temporal dead zone
22

33
With the introduction of `let` and `const` in **ES6** we can know better define our variable depending on our needs. Let's have a look at the major differences between them.
44

5-
&nbsp;
6-
75
## `Var`
86

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 globally.
7+
`var` are **function scoped**, which means that if we declare them inside a `for` loop \(which is a **block** scope\) they will be available globally.
108

11-
``` javascript
9+
```javascript
1210
for (var i = 0; i < 10; i++) {
1311
var global = "I am available globally";
1412
}
@@ -28,13 +26,11 @@ console.log(functionScoped);
2826

2927
In the first example the value of `var` global leaked out of the block-scope and could be accessed from the global scope whereas in the second example `var` was confined inside a function-scope and we could not access it from outside.
3028

31-
&nbsp;
32-
3329
## `Let`
3430

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.
31+
`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.
3632

37-
``` javascript
33+
```javascript
3834
// using `let`
3935
let x = "global";
4036

@@ -64,29 +60,22 @@ console.log(y);
6460

6561
As you can see, when we assigned a new value to our `let` inside our block-scope it **did not** change the value in the global scope, wherease when did the same with our `var` it leaked outside of the block-scope and also change it in the global scope.
6662

67-
&nbsp;
68-
6963
## `Const`
7064

71-
Similarly to `let`, `const` are **block-scoped** but they differ in the fact that their value **can't change through re-assignment or can't be redeclared**.
65+
Similarly to `let`, `const` are **block-scoped** but they differ in the fact that their value **can't change through re-assignment or can't be redeclared**.
7266

73-
74-
``` javascript
67+
```javascript
7568
const constant = 'I am a constant';
7669
constant = " I can't be reassigned";
7770

7871
// Uncaught TypeError: Assignment to constant variable
7972
```
8073

81-
82-
**Important**
83-
This **does not** mean that **const are immutable**.
84-
85-
&nbsp;
74+
**Important** This **does not** mean that **const are immutable**.
8675

8776
### The content of a `const` is an Object
8877

89-
``` javascript
78+
```javascript
9079
const person = {
9180
name: 'Alberto',
9281
age: 25,
@@ -95,16 +84,13 @@ const person = {
9584
person.age = 26;
9685

9786
// in this case no error will be raised, we are not re-assigning the variable but just one of its properties.
98-
```
99-
100-
---
101-
&nbsp;
87+
```
10288

10389
## The temporal dead zone
10490

10591
According to **MDN**:
10692

107-
> 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.
93+
> 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.
10894
10995
Let's look at an example:
11096

@@ -120,33 +106,27 @@ let j = "I am a let";
120106
// expected output: ReferenceError: can't access lexical declaration `j' before initialization
121107
```
122108

123-
`var` can be accessed **before** they are defined, but we can't access their **value**.
124-
`let` and `const` can't be accessed **before we define them**.
109+
`var` can be accessed **before** they are defined, but we can't access their **value**. `let` and `const` can't be accessed **before we define them**.
125110

126111
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.
127112

128-
129-
---
130-
&nbsp;
131-
132113
## When to use `Var`, `Let` and `Const`
133114

134115
There is no rule stating where to use each of them and people have different opinions. Here I am going to present to you two opinions from popular developers in the JavaScript community.
135116

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

119+
* use `const` by default
120+
* use `let` only if rebinding is needed.
121+
* `var` should never be used in ES6.
138122

139-
- use `const` by default
140-
- use `let` only if rebinding is needed.
141-
- `var` should never be used in ES6.
142-
123+
The second opinion comes from [Kyle Simpson:](https://github.com/AlbertoMontalesi/JavaScript-ES6-for-beginners-ebook/tree/33fc6a922b67c3f7e105bd14b3828b77a67ebdb4/ebook/blog.getify.com/constantly-confusing-const/README.md)
143124

144-
The second opinion comes from [Kyle Simpson:]( blog.getify.com/constantly-confusing-const/)
145-
146-
- Use `var` for top-level variables that are shared across many (especially larger) scopes.
147-
- Use `let` for localized variables in smaller scopes.
148-
- 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.
125+
* Use `var` for top-level variables that are shared across many \(especially larger\) scopes.
126+
* Use `let` for localized variables in smaller scopes.
127+
* 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.
149128

150129
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.
151130

152-
You may want to [read this article](https://medium.com/@sbakkila/javascript-es-6-let-and-the-dreaded-temporal-dead-zone-85b89314d168) to understand how `let` affects your performances compared to `var` before you choose to follow either [Mathias Bynes](https://mathiasbynens.be/notes/es6-const) or [Kyle Simpson]( blog.getify.com/constantly-confusing-const/).
131+
You may want to [read this article](https://medium.com/@sbakkila/javascript-es-6-let-and-the-dreaded-temporal-dead-zone-85b89314d168) to understand how `let` affects your performances compared to `var` before you choose to follow either [Mathias Bynes](https://mathiasbynens.be/notes/es6-const) or [Kyle Simpson](https://github.com/AlbertoMontalesi/JavaScript-ES6-for-beginners-ebook/tree/33fc6a922b67c3f7e105bd14b3828b77a67ebdb4/ebook/blog.getify.com/constantly-confusing-const/README.md).
132+

ebook/02_arrow_functions.md

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,50 @@
11
# Chapter 2: Arrow functions
22

3-
43
## What is an arrow function
54

6-
ES6 introduced fat arrows (`=>`) as a way to declare functions.
7-
This is how we would normally declare a function in ES5:
5+
ES6 introduced fat arrows \(`=>`\) as a way to declare functions. This is how we would normally declare a function in ES5:
86

9-
``` javascript
7+
```javascript
108
var greeting = (function(name) {
119
return "hello " + name;
1210
})
1311
```
1412

1513
The new syntax with a fat arrow looks like this:
1614

17-
``` javascript
15+
```javascript
1816
const greeting = (name) => {
1917
return `hello ${name}`;
2018
}
2119
```
2220

2321
But we can go further, if we only have one parameter we can drop the parenthesis and write:
2422

25-
``` javascript
23+
```javascript
2624
const greeting = name => {
2725
return `hello ${name}`;
2826
}
2927
```
3028

3129
And if we have no parameter at all we need to write empty parenthesis like this:
3230

33-
``` javascript
31+
```javascript
3432
const greeting = () => {
3533
return "hello";
3634
}
3735
```
3836

39-
40-
&nbsp;
41-
4237
## Implicitly return
4338

4439
With arrow functions we can skip the explicit return and return like this:
4540

46-
``` javascript
41+
```javascript
4742
const greeting = (name) => `hello ${name}` ;
4843
```
4944

5045
Let's say we want to implicitly return an **object literal**, we would do like this:
5146

52-
``` javascript
47+
```javascript
5348
const race = "100m dash";
5449
const runners = [ "Usain Bolt", "Justin Gatlin", "Asafa Powell" ];
5550

@@ -59,31 +54,24 @@ console.log(winner);
5954
// 0: {name: "Usain Bolt", race: "100m dash", place: 1}
6055
// 1: {name: "Justin Gatlin", race: "100m dash", place: 2}
6156
// 2: {name: "Asafa Powell", race: "100m dash", place: 3}
62-
6357
```
6458

6559
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.
6660

6761
Writing `race` or `race:race` is the same.
6862

69-
&nbsp;
70-
7163
## Arrow functions are anonymous
7264

7365
As you can see from the previous examples, arrow functions are **anonymous**.
7466

7567
If we want to have a name to reference them we can bind them to a variable:
7668

77-
``` javascript
69+
```javascript
7870
const greeting = (name) => `hello ${name}`;
7971

8072
greeting("Tom");
8173
```
8274

83-
84-
85-
&nbsp;
86-
8775
## Arrow function and the `this` keyword
8876

8977
You need to be careful when using arrow functions in conjunction with the this keyword as they behave differently from normal functions.
@@ -92,7 +80,7 @@ When you use an arrow function, the `this` keyword is inherited from the parent
9280

9381
This can be useful in cases like this one:
9482

95-
``` javascript
83+
```javascript
9684
// grab our div with class box
9785
const box = document.querySelector(".box");
9886
// listen for a click event
@@ -106,16 +94,15 @@ box.addEventListener("click",function() {
10694
})
10795
```
10896

109-
11097
The problem in this case is that the first `this` is bound to the `const` box but the second one, inside the `setTimeout` will be set to the `Window` object, trowing this error:
11198

112-
``` javascript
113-
Uncaught TypeError: cannot read property "toggle" of undefined
99+
```javascript
100+
Uncaught TypeError: cannot read property "toggle" of undefined
114101
```
115102

116103
Since we know that **arrow functions** inherit the value of this from the parent scope, we can re-write our function like this:
117104

118-
``` javascript
105+
```javascript
119106
// grab our div with class box
120107
const box = document.querySelector(".box");
121108
// listen for a click event
@@ -131,24 +118,21 @@ box.addEventListener("click",function() {
131118

132119
Here, the second `this` will inherit from its parent, and will be therefore set to the `const` box.
133120

134-
135-
&nbsp;
136-
137121
## When you should avoid arrow functions
138122

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

141125
The next 2 examples all show when to be careful using `this` inside of arrows.
142126

143-
``` javascript
127+
```javascript
144128
const button = document.querySelector("btn");
145129
button.addEventListener("click", () => {
146130
// error: *this* refers to the window
147131
this.classList.toggle("on");
148132
})
149133
```
150134

151-
``` javascript
135+
```javascript
152136
const person = {
153137
age: 10,
154138
grow: () => {
@@ -160,7 +144,7 @@ const person = {
160144

161145
Here's another example of when you should use a normal function instead of an arrow.
162146

163-
``` javascript
147+
```javascript
164148
const orderRunners = () => {
165149
const runners = Array.from(arguments);
166150
return runners.map((runner, i) => {
@@ -172,8 +156,9 @@ const orderRunners = () => {
172156

173157
This code will return:
174158

175-
``` javascript
159+
```javascript
176160
ReferenceError: arguments is not defined
177161
```
178162

179-
We don't have access to the `arguments` object in arrow functions, we need to use a normal function.
163+
We don't have access to the `arguments` object in arrow functions, we need to use a normal function.
164+

0 commit comments

Comments
 (0)