You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-6Lines changed: 5 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,20 @@
1
1
# JavaScript ES6 for beginners
2
2
3
-
<imgsrc="assets/cover.png"width="300">
3
+

4
4
5
5
## Disclaimer
6
6
7
7
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...
8
8
9
9
## About me
10
10
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/).
13
12
14
13
## Contributions & Donations
15
14
16
15
Any contributions you make are of course greatly appreciated.
17
16
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).
19
18
20
19
## Table of contents
21
20
@@ -36,7 +35,7 @@ If you enjoy my content and you want to donate me a cup of coffee, you can do [h
Copy file name to clipboardExpand all lines: ebook/01_var_let_const.md
+21-41Lines changed: 21 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
2
2
3
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.
4
4
5
-
6
-
7
5
## `Var`
8
6
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.
10
8
11
-
```javascript
9
+
```javascript
12
10
for (var i =0; i <10; i++) {
13
11
varglobal="I am available globally";
14
12
}
@@ -28,13 +26,11 @@ console.log(functionScoped);
28
26
29
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.
30
28
31
-
32
-
33
29
## `Let`
34
30
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.
36
32
37
-
```javascript
33
+
```javascript
38
34
// using `let`
39
35
let x ="global";
40
36
@@ -64,29 +60,22 @@ console.log(y);
64
60
65
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, wherease when did the same with our `var` it leaked outside of the block-scope and also change it in the global scope.
66
62
67
-
68
-
69
63
## `Const`
70
64
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**.
72
66
73
-
74
-
```javascript
67
+
```javascript
75
68
constconstant='I am a constant';
76
69
constant =" I can't be reassigned";
77
70
78
71
// Uncaught TypeError: Assignment to constant variable
79
72
```
80
73
81
-
82
-
**Important**
83
-
This **does not** mean that **const are immutable**.
84
-
85
-
74
+
**Important** This **does not** mean that **const are immutable**.
86
75
87
76
### The content of a `const` is an Object
88
77
89
-
```javascript
78
+
```javascript
90
79
constperson= {
91
80
name:'Alberto',
92
81
age:25,
@@ -95,16 +84,13 @@ const person = {
95
84
person.age=26;
96
85
97
86
// 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
-
87
+
```
102
88
103
89
## The temporal dead zone
104
90
105
91
According to **MDN**:
106
92
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.
`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**.
125
110
126
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.
127
112
128
-
129
-
---
130
-
131
-
132
113
## When to use `Var`, `Let` and `Const`
133
114
134
115
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.
135
116
136
117
The first opinion comes from [Mathias Bynes:](https://mathiasbynens.be/notes/es6-const)
137
118
119
+
* use `const` by default
120
+
* use `let` only if rebinding is needed.
121
+
*`var` should never be used in ES6.
138
122
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)
143
124
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.
149
128
150
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.
151
130
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).
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.
66
60
67
61
Writing `race` or `race:race` is the same.
68
62
69
-
70
-
71
63
## Arrow functions are anonymous
72
64
73
65
As you can see from the previous examples, arrow functions are **anonymous**.
74
66
75
67
If we want to have a name to reference them we can bind them to a variable:
76
68
77
-
```javascript
69
+
```javascript
78
70
constgreeting= (name) =>`hello ${name}`;
79
71
80
72
greeting("Tom");
81
73
```
82
74
83
-
84
-
85
-
86
-
87
75
## Arrow function and the `this` keyword
88
76
89
77
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
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:
0 commit comments