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: ebook/01_var_let_const.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ With the introduction of `let` and `const` in **ES6**, we can now better define
6
6
7
7
## `Var`
8
8
9
-
Variables declared with the `var` keyword are **function scoped**, which means that if we declare them inside a `for` loop (which is a **block** scope) they will be available even outside of it.
9
+
Variables declared with the `var` keyword are **function scoped**, which means that if we declare them inside a `for` loop (which is a **block** scope), they will be available even outside of it.
10
10
11
11
```JavaScript
12
12
for (var i =0; i <10; i++) {
@@ -26,7 +26,7 @@ console.log(functionScoped);
26
26
// ReferenceError: functionScoped is not defined
27
27
```
28
28
29
-
In the first example the value of the `var` leaked out of the block-scope and could be accessed from outside of it, 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` leaked out of the block-scope and could be accessed from outside of it, whereas in the second example `var` was confined inside a function-scope, and we could not access it from outside.
30
30
31
31
32
32
@@ -62,13 +62,13 @@ console.log(y);
62
62
// expected output: block-scoped
63
63
```
64
64
65
-
As you can see, when we assigned a new value to the variable declared with `let` inside our block-scope, it **did not** change its value in the outer scope. Whereas, when we did the same with the variable declared with var, it leaked outside of the block-scope and also changed it in the outer scope.
65
+
As you can see, when we assigned a new value to the variable declared with `let` inside our blockscope, it **did not** change its value in the outer scope. Whereas, when we did the same with the variable declared with var, it leaked outside of the blockscope and also changed it in the outer scope.
66
66
67
67
68
68
69
69
## `Const`
70
70
71
-
Similarly to `let`, variables declared with `const` are also **block-scoped**, but they differ in the fact that their value **can't change through re-assignment or can't be re-declared**.
71
+
Similarly to `let`, variables declared with `const` are also **blockscoped**, but they differ in the fact that their value **can't change through re-assignment and can't be re-declared**.
72
72
73
73
```JavaScript
74
74
constconstant='I am a constant';
@@ -82,7 +82,7 @@ This **does not** mean that variables declared with `const` are immutable.
82
82
83
83
84
84
85
-
### The content of a `const` is an Object
85
+
### The content of a `const` is an object
86
86
87
87
```JavaScript
88
88
constperson= {
@@ -95,11 +95,11 @@ console.log(person.age);
95
95
// 26
96
96
```
97
97
98
-
In this case we are not reassigning the whole variable but just one of its properties, which is perfectly fine.
98
+
In this case we are not reassigning the whole variable but just one of its properties, which works fine fine.
99
99
100
100
---
101
101
102
-
>Note: We can still freeze the `const` object, which will not change the contents of the object (but trying to change the values of object `JavaScript` will not throw any error)
102
+
>Note: We can still freeze the `const` object, which will not change the contents of the object (but trying to change the values of object `JavaScript` will not throw any error).
103
103
104
104
```JavaScript
105
105
constperson= {
@@ -123,7 +123,7 @@ console.log(person.age);
123
123
124
124
## The temporal dead zone
125
125
126
-
Now we will have a look at a very important concept which may sound complicated from its name, but I assure you it is not.
126
+
Now, we’ll have a look at a very important concept which may sound complicated because of its name, but I assure you it's not.
127
127
128
128
First let's have a look at a simple example:
129
129
@@ -142,7 +142,7 @@ let j = "I am a let";
142
142
`var` can be accessed **before** they are defined, but we can't access their **value**.
143
143
`let` and `const` can't be accessed **before we define them**.
144
144
145
-
Despite what you may read on other sources, both `var` and `let`(and `const`) are subject to **hoisting** which means that they are processed before any code is executed and lifted up to the top of their scope (whether it's global or block).
145
+
Despite what you may read on other sources, both `var` and `let`(and `const`) are subject to **hoisting**, which means that they are processed before any code is executed and lifted up to the top of their scope (whether it's global or block).
146
146
147
147
The main differences lie in the fact that `var` can still be accessed before they are defined. This causes the value to be `undefined`. While on the other hand, `let` lets the variables sit in a **temporal dead zone** until they are declared. And this causes an error when accessed before initialization, which makes it easier to debug code rather than having an `undefined` as the result.
148
148
@@ -151,7 +151,7 @@ The main differences lie in the fact that `var` can still be accessed before the
151
151
152
152
## When to use `Var`, `Let` and `Const`
153
153
154
-
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.
154
+
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.
155
155
156
156
The first opinion comes from [Mathias Bynes:](https://mathiasbynens.be/notes/es6-const)
In this example, we are using the `map` function to iterate over the array `runners`. The first argument is the current item in the array and the `i` is the index of it. For each item in the array we are then adding into `results` an Object containing the properties `name`, `race`, and `place`.
81
+
In this example, we're using the `map` function to iterate over the array `runners`. The first argument is the current item in the array, and the `i` is the index of it. For each item in the array we are then adding into `results` an Object containing the properties `name`, `race`, and `place`.
82
82
83
-
To tell `JavaScript` what's inside the curly braces is an **object literal** we want to implicitly return, we need to wrap everything inside parenthesis.
83
+
To tell `JavaScript` what's inside the curly braces is an **object literal** we want to implicitly return, so we need to wrap everything inside parentheses. Writing `race` or `race: race` is the same.
Here, the second `this` will inherit from its parent, and will be set to the `const` box.
159
159
160
-
Running the example code you should see our `div` turning red for just half a second.
160
+
Running the example code, you should see our `div` turning red for just half a second.
161
161
162
162
163
163
@@ -205,7 +205,7 @@ const person2 = {
205
205
person2.grow();
206
206
```
207
207
208
-
Another difference between Arrow functions and normal functions is the access to the `arguments object`.
208
+
Another difference between arrow functions and normal functions is the access to the `arguments object`.
209
209
The `arguments object` is an array-like object that we can access from inside functions and contains the values of the arguments passed to that function.
210
210
211
211
A quick example:
@@ -221,7 +221,7 @@ example(1,2,3);
221
221
222
222
As you can see we accessed the first argument using an array notation `arguments[0]`.
223
223
224
-
Similarly to what we saw with the `this` keyword, Arrow functions inherit the value of the `arguments object` from their parent scope.
224
+
Similarly to what we saw with the `this` keyword, arrow functions inherit the value of the `arguments object` from their parent scope.
225
225
226
226
Let's have a look at this example with our previous list of runners:
227
227
@@ -240,7 +240,7 @@ This code will return:
240
240
ReferenceError:arguments is not defined
241
241
```
242
242
243
-
To access all the arguments passed to the function we can either use the old function notation or the spread syntax(which we will discuss more in Chapter 9)
243
+
To access all the arguments passed to the function we can either use the old function notation or the spread syntax(which we will discuss more in Chapter 9)
244
244
245
245
Remember that `arguments` is just a keyword, it's not a variable name.
Copy file name to clipboardExpand all lines: ebook/03_default_function_arguments.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ getLocation('Paris','France')
20
20
// Europe France Paris
21
21
```
22
22
23
-
As you can see our function takes three arguments, a city, a country and a continent. In the function body we are checking if either country or continent are `undefined` and in that case we are giving them a **default value**.
23
+
As you can see, our function takes three arguments: a city, a country and a continent. In the function body we are checking if either country or continent are `undefined` and in that case, we are giving them a **default value**.
24
24
25
25
When calling `getLocation('Milan')` the second and third parameter (country and continent) are undefined and get replaced by the default values of our functions.
If we want to replace any of the first arguments with our default we need to pass them as `undefined`, not really a nice looking solution. Luckily ES6 came to the rescue with default function arguments.
47
+
If we want to replace any of the first arguments with our default we need to pass them as `undefined`, which is not the nicest looking solution. Luckily, ES6 came to the rescue with default function arguments.
We made the argument of our function an Object. When calling the function, we don’t even have to worry about the order of the parameters because they are matched based on their key.
93
93
94
-
In the example above the default value for *tip* was 0.05 and we overwrote it with 0.15 but we didn't give a value to tax which remained the default 0.1.
94
+
In the example above, the default value for *tip* was 0.05, and we overwrote it with 0.15. But we didn’t give a value to tax which remained the default 0.1.
95
95
96
96
Notice this detail:
97
97
@@ -103,7 +103,7 @@ Notice this detail:
103
103
} = {}
104
104
```
105
105
106
-
If we don't default our argument Object to an empty Object, and we were to try and run `calculatePrice()` we would get:
106
+
If we don't default our argument Object to an empty Object, and we were to try and run `calculatePrice()`, we would get:
107
107
108
108
```JavaScript
109
109
Cannot destructure property `total`of'undefined' or 'null'.
Copy file name to clipboardExpand all lines: ebook/04_template_literals.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
1
# Chapter 4: Template literals
2
2
3
-
Prior to ES6 they were called *template strings*, now we call them *template literals*. Let's have a look at what changed in the way we interpolate strings in ES6.
3
+
*Template literals*were called *template strings prior* to ES6.. Let’s have a look at what’s changed in the way we interpolate strings in ES6.
4
4
5
5
6
6
7
7
## Interpolating strings
8
8
9
-
In ES5 we used to write this, in order to interpolate strings:
9
+
We used to write the following in ES5 in order to interpolate strings:
10
10
11
11
```JavaScript
12
12
var name ="Alberto";
@@ -16,7 +16,7 @@ console.log(greeting);
16
16
// Hello my name is Alberto
17
17
```
18
18
19
-
In ES6 we can use backticks to make our lives easier.
19
+
In ES6m we can use backticks to make our lives easier.
20
20
21
21
```JavaScript
22
22
let name ="Alberto";
@@ -40,7 +40,7 @@ console.log('1 * 10 is ' + ( a * b));
40
40
41
41
```
42
42
43
-
In ES6 we can use backticks to reduce our typing:
43
+
In ES6 we simply have to wrap everything inside backticks, so backslashes on each line aren’t needed anymore.
44
44
45
45
```JavaScript
46
46
var a =1;
@@ -100,7 +100,7 @@ console.log(markup);
100
100
// </ul>
101
101
```
102
102
103
-
Here we are using the `map` function to loop over each of our `people` and display a `li` tag containing the `name` of the person.
103
+
Here, we're using the `map` function to loop over each of our `people` and display a `li` tag containing the `name` of the person.
104
104
105
105
106
106
@@ -120,7 +120,7 @@ getPrice();
120
120
// $20
121
121
```
122
122
123
-
If the condition before the `?` can be converted to `true` then the first value is returned, else it's the value after the `:` that gets returned.
123
+
If the condition before the `?` can be converted to `true` then the first value is returned. Otherwise, it's the value after the `:` that gets returned.
124
124
125
125
```javascript
126
126
// create an artist with name and age
@@ -155,7 +155,7 @@ const artist = {
155
155
156
156
## Pass a function inside a template literal
157
157
158
-
Similarly to the example above (line 10 of the code), if we want to, we can pass a function inside a template literal.
158
+
Similarly to the example above (line 10 of the code), we can pass a function inside a template literal.
159
159
160
160
```javascript
161
161
constgroceries= {
@@ -197,16 +197,16 @@ const markup = `
197
197
// <div>
198
198
```
199
199
200
-
Inside of the last `p` tag we are calling our function `groceryList` passing it all the `others` groceries as an argument.
201
-
Inside of the function we are returning a `p` tag and we are using `map` to loop over each of our items in the grocery listreturning an Array of `span` tags containing each grocery. We are then using `.join('\n')` to add a new line after each of those span.
200
+
Inside of the last `p` tag, we're calling our function `groceryList` and passing it all the `others` groceries as an argument.
201
+
Inside of the function, we're returning a `p` tag and are using `map` to loop over each of our items in the grocery list. This includes returning an array of `span` tags containing each grocery. We're then using `.join('\n')` to add a new line after each of those spans.
202
202
203
203
204
204
205
205
## Tagged template literals
206
206
207
-
By tagging a function to a template literal we can run the template literal through the function, providing it with everything that's inside of the template.
207
+
By tagging a function to a template literal, we can run the template literal through the function, providing it with everything that's inside of the template.
208
208
209
-
The way it works is very simple: you just take the name of your function and put it in front of the template that you want to run it against.
209
+
The way it works is very simple: you take the name of your function and put it in front of the template that you want to run it against.
210
210
211
211
```javascript
212
212
let person ="Alberto";
@@ -227,9 +227,9 @@ console.log(sentence);
227
227
```
228
228
229
229
We captured the value of the variable age and used a ternary operator to decide what to print.
230
-
`strings` will take all the strings of our `let` sentence whilst the other parameters will hold the variables.
230
+
`strings` will take all the strings of our `let` sentence, while the other parameters will hold the variables.
231
231
232
-
In our example our string is divided in 3 pieces: `${person}`, `is a` and `${age}`.
232
+
In our example the string is divided into 3 pieces: `${person}`, `is a` and `${age}`.
233
233
We use array notation to access the string in the middle like this:
Copy file name to clipboardExpand all lines: ebook/05_additional_string_methods.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ str.toLowerCase()
42
42
// Output: "i ate an apple"
43
43
```
44
44
45
-
There are many more methods; these were just a few as a reminder. Check the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#) for a more in-depth description on the above methods.
45
+
There are many more methods- these were just a few as a reminder. Check the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#) for a more in-depth description of the above methods.
46
46
47
47
48
48
@@ -85,7 +85,7 @@ code.startsWith("DEF",3);
85
85
86
86
### `endsWith()`
87
87
88
-
Similarly to `startsWith()` this new method will check if the string ends with the value we pass in:
88
+
Similarly to `startsWith()`, this new method will check if the string ends with the value we pass in:
Copy file name to clipboardExpand all lines: ebook/06_destructuring.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,7 @@ const person = {
33
33
const { first, last } = person;
34
34
```
35
35
36
-
Since our `const` variable,`person`, have the same name as the properties we want to grab, we don't have to specify `person.first` and `person.last` anymore.
36
+
Since our `const` variable-`person`- has the same name as the properties we want to grab, we don't have to specify `person.first` and `person.last` anymore.
37
37
38
38
The same applies even when we have nested data, such as what we could get from an API.
39
39
@@ -52,7 +52,7 @@ const person = {
52
52
const { facebook } =person.links.social;
53
53
```
54
54
55
-
We are not limited to name our variable the same as the property of the object. We can also rename as the following:
55
+
We're not limited to name our variables the same as the property of the object. We can also rename as the following:
console.log(facebook); //ReferenceError: facebook is not defined
62
62
```
63
63
64
-
We are using the syntax `const { facebook:fb }` to specify that we want to target the property `facebook` of the Object`person.links.social` and we want the `const` variable to be called `fb`.
64
+
We're using the syntax `const { facebook:fb }` to specify that we want to target the property `facebook` of the object`person.links.social`, and that we want the `const` variable to be called `fb`.
65
65
That is why when we try to log `facebook` we get an error.
In the example above the first two values of the array were bound to `name` and `surname` while the rest (that's why it's called the **rest operator**) get assigned to `food`
106
+
In the example above, the first two values of the array were bound to `name` and `surname`, while the rest (that's why it's called the **rest operator**) get assigned to `food`
107
107
108
108
The `...` is the syntax for the **rest operator**.
0 commit comments