Skip to content

Commit cb65d7d

Browse files
spellcheck
1 parent a214ac4 commit cb65d7d

16 files changed

+422
-233
lines changed

ebook/01_var_let_const.md

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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 know better define our variable 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 variable depending on our needs. Let's have a look at the major differences between them.
4+
5+
 
46

57
## `Var`
68

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

9-
```javascript
11+
``` javascript
1012
for (var i = 0; i < 10; i++) {
1113
var global = "I am available globally";
1214
}
@@ -24,13 +26,15 @@ console.log(functionScoped);
2426
// ReferenceError: functionScoped is not defined
2527
```
2628

27-
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.
29+
In the first example the value of the `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.
30+
31+
&nbsp;
2832

2933
## `Let`
3034

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

33-
```javascript
37+
``` javascript
3438
// using `let`
3539
let x = "global";
3640

@@ -58,24 +62,31 @@ console.log(y);
5862
// expected output: block-scoped
5963
```
6064

61-
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, whereas when did the same with our `var` it leaked outside of the block-scope and also change 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 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.
66+
67+
&nbsp;
6268

6369
## `Const`
6470

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**.
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**.
6672

67-
```javascript
73+
74+
``` javascript
6875
const constant = 'I am a constant';
6976
constant = " I can't be reassigned";
7077

7178
// Uncaught TypeError: Assignment to constant variable
7279
```
7380

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

7687
### The content of a `const` is an Object
7788

78-
```javascript
89+
``` javascript
7990
const person = {
8091
name: 'Alberto',
8192
age: 25,
@@ -84,13 +95,16 @@ const person = {
8495
person.age = 26;
8596

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

89103
## The temporal dead zone
90104

91105
According to **MDN**:
92106

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.
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.
94108
95109
Let's look at an example:
96110

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

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**.
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**.
125+
126+
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.
110127

111-
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+
129+
---
130+
&nbsp;
112131

113132
## When to use `Var`, `Let` and `Const`
114133

115134
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.
116135

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

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

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)
139+
- use `const` by default
140+
- use `let` only if rebinding is needed.
141+
- `var` should never be used in ES6.
124142

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

129-
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.
144+
The second opinion comes from [Kyle Simpson:]( blog.getify.com/constantly-confusing-const/)
130145

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).
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.
149+
150+
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.
132151

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

ebook/02_arrow_functions.md

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,53 @@
22

33
## What is an arrow function
44

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

7-
```javascript
8+
``` javascript
89
var greeting = (function(name) {
910
return "hello " + name;
1011
})
1112
```
1213

1314
The new syntax with a fat arrow looks like this:
1415

15-
```javascript
16+
``` javascript
1617
const greeting = (name) => {
1718
return `hello ${name}`;
1819
}
1920
```
2021

21-
But we can go further, if we only have one parameter we can drop the parenthesis and write:
22+
We can go further, if we only have one parameter we can drop the parenthesis and write:
2223

23-
```javascript
24+
``` javascript
2425
const greeting = name => {
2526
return `hello ${name}`;
2627
}
2728
```
2829

29-
And if we have no parameter at all we need to write empty parenthesis like this:
30+
If we have no parameter at all we need to write empty parenthesis like this:
3031

31-
```javascript
32+
``` javascript
3233
const greeting = () => {
3334
return "hello";
3435
}
3536
```
3637

38+
39+
&nbsp;
40+
3741
## Implicitly return
3842

3943
With arrow functions we can skip the explicit return and return like this:
4044

41-
```javascript
45+
``` javascript
4246
const greeting = (name) => `hello ${name}` ;
4347
```
4448

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

47-
```javascript
51+
``` javascript
4852
const race = "100m dash";
4953
const runners = [ "Usain Bolt", "Justin Gatlin", "Asafa Powell" ];
5054

@@ -54,33 +58,39 @@ console.log(winner);
5458
// 0: {name: "Usain Bolt", race: "100m dash", place: 1}
5559
// 1: {name: "Justin Gatlin", race: "100m dash", place: 2}
5660
// 2: {name: "Asafa Powell", race: "100m dash", place: 3}
61+
5762
```
5863

59-
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.
64+
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.
6065

6166
Writing `race` or `race:race` is the same.
6267

68+
&nbsp;
69+
6370
## Arrow functions are anonymous
6471

6572
As you can see from the previous examples, arrow functions are **anonymous**.
6673

6774
If we want to have a name to reference them we can bind them to a variable:
6875

69-
```javascript
76+
``` javascript
7077
const greeting = (name) => `hello ${name}`;
7178

7279
greeting("Tom");
7380
```
7481

82+
83+
&nbsp;
84+
7585
## Arrow function and the `this` keyword
7686

77-
You need to be careful when using arrow functions in conjunction with the this keyword as they behave differently from normal functions.
87+
You need to be careful when using arrow functions in conjunction with the this keyword, as they behave differently from normal functions.
7888

7989
When you use an arrow function, the `this` keyword is inherited from the parent scope.
8090

8191
This can be useful in cases like this one:
8292

83-
```javascript
93+
``` javascript
8494
// grab our div with class box
8595
const box = document.querySelector(".box");
8696
// listen for a click event
@@ -94,15 +104,16 @@ box.addEventListener("click",function() {
94104
})
95105
```
96106

97-
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:
98107

99-
```javascript
100-
Uncaught TypeError: cannot read property "toggle" of undefined
108+
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:
109+
110+
``` javascript
111+
Uncaught TypeError: cannot read property "toggle" of undefined
101112
```
102113

103-
Since we know that **arrow functions** inherit the value of this from the parent scope, we can re-write our function like this:
114+
Since we know that **arrow functions** inherit the value of `this` from the parent scope, we can re-write our function like this:
104115

105-
```javascript
116+
``` javascript
106117
// grab our div with class box
107118
const box = document.querySelector(".box");
108119
// listen for a click event
@@ -118,21 +129,24 @@ box.addEventListener("click",function() {
118129

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

132+
133+
&nbsp;
134+
121135
## When you should avoid arrow functions
122136

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

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

127-
```javascript
141+
``` javascript
128142
const button = document.querySelector("btn");
129143
button.addEventListener("click", () => {
130144
// error: *this* refers to the window
131145
this.classList.toggle("on");
132146
})
133147
```
134148

135-
```javascript
149+
``` javascript
136150
const person = {
137151
age: 10,
138152
grow: () => {
@@ -144,7 +158,7 @@ const person = {
144158

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

147-
```javascript
161+
``` javascript
148162
const orderRunners = () => {
149163
const runners = Array.from(arguments);
150164
return runners.map((runner, i) => {
@@ -156,9 +170,8 @@ const orderRunners = () => {
156170

157171
This code will return:
158172

159-
```javascript
173+
``` javascript
160174
ReferenceError: arguments is not defined
161175
```
162176

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

ebook/03_default_function_arguments.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
ES6 makes it very easy to set default function arguments. Let's look at an example:
66

7-
```javascript
7+
``` javascript
88
function calculatePrice(total, tax = 0.1, tip = 0.05){
99
// When no value is given for tax or tip, the default 0.1 and 0.05 will be used
1010
return total + (total * tax) + (total * tip);,
@@ -13,14 +13,14 @@ return total + (total * tax) + (total * tip);,
1313

1414
What if we don't want to pass the parameter at all, like this:
1515

16-
```javascript
16+
``` javascript
1717
// The 0.15 will be bound to the second argument, tax even if in our intention it was to set 0.15 as the tip
1818
calculatePrice(100, 0.15)
1919
```
2020

2121
We can solve by doing this:
2222

23-
```javascript
23+
``` javascript
2424
// In this case 0.15 will be bound to the tip
2525
calculatePrice(100, undefined, 0.15)
2626
```
@@ -29,11 +29,10 @@ It works, but it's not very nice, how to improve it?
2929

3030
With **destructuring** we can write this:
3131

32-
```javascript
32+
``` javascript
3333
const Bill = calculatePrice({ tip: 0.15, total:150});
3434
```
3535

3636
We don't even have to pass the parameters in the same order as when we declared our function, since we are calling them the same way as the arguments JavaScript will know how to match them.
3737

38-
Don't worry about destructuring, we will talk about it in a later chapter.
39-
38+
Don't worry about destructuring, we will talk about it in a later chapter.

0 commit comments

Comments
 (0)