Skip to content

Commit 7f54cdf

Browse files
committed
� Conflicts: � 1-js/02-first-steps/04-variables/article.md � 1-js/06-advanced-functions/04-var/article.md � 2-ui/2-events/02-bubbling-and-capturing/article.md
2 parents 9ee4c59 + cd9c81c commit 7f54cdf

18 files changed

Lines changed: 180 additions & 92 deletions

File tree

1-js/02-first-steps/08-operators/3-primitive-conversions-questions/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ true + false = 1
1010
"4" - 2 = 2
1111
"4px" - 2 = NaN
1212
7 / 0 = Infinity
13-
" -9 " + 5 = " -9 5" // (3)
14-
" -9 " - 5 = -14 // (4)
13+
" -9 " + 5 = " -9 5" // (3)
14+
" -9 " - 5 = -14 // (4)
1515
null + 1 = 1 // (5)
1616
undefined + 1 = NaN // (6)
1717
" \t \n" - 2 = -2 // (7)

1-js/02-first-steps/12-nullish-coalescing-operator/article.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,40 @@ x = (a !== null && a !== undefined) ? a : b;
1616

1717
Here's a longer example.
1818

19-
Let's say, we have a `firstName`, `lastName` or `nickName`, all of them optional.
19+
Imagine, we have a user, and there are variables `firstName`, `lastName` or `nickName` for their first name, last name and the nick name. All of them may be undefined, if the user decided not to enter any value.
2020

21-
Let's choose the defined one and show it (or "Anonymous" if nothing is set):
21+
We'd like to display the user name: one of these three variables, or show "Anonymous" if nothing is set.
22+
23+
Let's use the `??` operator to select the first defined one:
2224

2325
```js run
2426
let firstName = null;
2527
let lastName = null;
2628
let nickName = "Supercoder";
2729

28-
// show the first not-null/undefined variable
30+
// show the first not-null/undefined value
31+
*!*
2932
alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder
33+
*/!*
3034
```
3135
3236
## Comparison with ||
3337
34-
That's very similar to OR `||` operator. Actually, we can replace `??` with `||` in the code above and get the same result.
38+
The OR `||` operator can be used in the same way as `??`. Actually, we can replace `??` with `||` in the code above and get the same result, as it was described in the [previous chapter](info:logical-operators#or-finds-the-first-truthy-value).
3539
3640
The important difference is that:
3741
- `||` returns the first *truthy* value.
3842
- `??` returns the first *defined* value.
3943
4044
This matters a lot when we'd like to treat `null/undefined` differently from `0`.
4145
42-
For example:
46+
For example, consider this:
4347
4448
```js
4549
height = height ?? 100;
4650
```
4751
48-
This sets `height` to `100` if it's not defined. But if `height` is `0`, then it remains "as is".
52+
This sets `height` to `100` if it's not defined.
4953
5054
Let's compare it with `||`:
5155
@@ -56,17 +60,19 @@ alert(height || 100); // 100
5660
alert(height ?? 100); // 0
5761
```
5862
59-
Here, `height || 100` treats zero height as unset, same as `null`, `undefined` or any other falsy value, depeding on use cases that may be incorrect.
63+
Here, `height || 100` treats zero height as unset, same as `null`, `undefined` or any other falsy value. So the result is `100`.
64+
65+
The `height ?? 100` returns `100` only if `height` is exactly `null` or `undefined`. So the `alert` shows the height value `0` "as is".
6066
61-
The `height ?? 100` returns `100` only if `height` is exactly `null` or `undefined`.
67+
Which behavior is better depends on a particular use case. When zero height is a valid value, then `??` is preferrable.
6268
6369
## Precedence
6470
6571
The precedence of the `??` operator is rather low: `7` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
6672
67-
That's lower than most operators and a bit higher than `=` and `?`.
73+
So `??` is evaluated after most other operations, but before `=` and `?`.
6874
69-
So if we need to use `??` in a complex expression, then consider adding parentheses:
75+
If we need to choose a value with `??` in a complex expression, then consider adding parentheses:
7076
7177
```js run
7278
let height = null;
@@ -78,27 +84,34 @@ let area = (height ?? 100) * (width ?? 50);
7884
alert(area); // 5000
7985
```
8086
81-
Otherwise, if we omit parentheses, then `*` has the higher precedence and would run first. That would be the same as:
87+
Otherwise, if we omit parentheses, `*` has the higher precedence than `??` and would run first.
88+
89+
That would work be the same as:
8290
8391
```js
84-
// not correct
92+
// probably not correct
8593
let area = height ?? (100 * width) ?? 50;
8694
```
8795
88-
There's also a related language-level limitation. Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.
96+
There's also a related language-level limitation.
97+
98+
**Due to safety reasons, it's forbidden to use `??` together with `&&` and `||` operators.**
8999
90100
The code below triggers a syntax error:
91101
92102
```js run
93103
let x = 1 && 2 ?? 3; // Syntax error
94104
```
95105
96-
The limitation is surely debatable, but for some reason it was added to the language specification.
106+
The limitation is surely debatable, but it was added to the language specification with the purpose to avoid programming mistakes, as people start to switch to `??` from `||`.
97107
98-
Use explicit parentheses to fix it:
108+
Use explicit parentheses to work around it:
99109
100110
```js run
111+
*!*
101112
let x = (1 && 2) ?? 3; // Works
113+
*/!*
114+
102115
alert(x); // 2
103116
```
104117

1-js/03-code-quality/02-coding-style/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ Of course, a team can always write their own style guide, but usually there's no
285285
286286
Some popular choices:
287287
288-
- [Google JavaScript Style Guide](https://google.github.io/styleguide/javascriptguide.xml)
288+
- [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)
289289
- [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript)
290290
- [Idiomatic.JS](https://github.com/rwaldron/idiomatic.js)
291291
- [StandardJS](https://standardjs.com/)

1-js/03-code-quality/05-testing-mocha/beforeafter.view/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
describe("test", function() {
2+
3+
// Mocha usually waits for the tests for 2 seconds before considering them wrong
4+
5+
this.timeout(200000); // With this code we increase this - in this case to 200,000 milliseconds
26

7+
// This is because of the "alert" function, because if you delay pressing the "OK" button the tests will not pass!
8+
39
before(() => alert("Testing started – before all tests"));
410
after(() => alert("Testing finished – after all tests"));
511

1-js/05-data-types/10-destructuring-assignment/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ alert(`${guest} ${admin}`); // Pete Jane (successfully swapped!)
138138
Here we create a temporary array of two variables and immediately destructure it in swapped order.
139139

140140
We can swap more than two variables this way.
141-
```
141+
142142

143143
### The rest '...'
144144

1-js/06-advanced-functions/02-rest-parameters-spread/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ So, for the task of turning something into an array, `Array.from` tends to be mo
227227
228228
## Get a new copy of an array/object
229229
230-
Remember when we talked about `Object.assign()` [in the past](https://javascript.info/object#cloning-and-merging-object-assign)?
230+
Remember when we talked about `Object.assign()` [in the past](info:object-copy#cloning-and-merging-object-assign)?
231231
232232
It is possible to do the same thing with the spread syntax.
233233
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function byField(fieldName){
2+
return (a, b) => a[fieldName] > b[fieldName] ? 1 : -1;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function byField(fieldName){
2+
3+
// Your code goes here.
4+
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
describe("byField", function(){
2+
3+
let users = [
4+
{ name: "John", age: 20, surname: "Johnson" },
5+
{ name: "Pete", age: 18, surname: "Peterson" },
6+
{ name: "Ann", age: 19, surname: "Hathaway" },
7+
];
8+
9+
it("sorts users by name", function(){
10+
let nameSortedKey = [
11+
{ name: "Ann", age: 19, surname: "Hathaway" },
12+
{ name: "John", age: 20, surname: "Johnson"},
13+
{ name: "Pete", age: 18, surname: "Peterson" },
14+
];
15+
let nameSortedAnswer = users.sort(byField("name"));
16+
assert.deepEqual(nameSortedKey, nameSortedAnswer);
17+
});
18+
19+
it("sorts users by age", function(){
20+
let ageSortedKey = [
21+
{ name: "Pete", age: 18, surname: "Peterson" },
22+
{ name: "Ann", age: 19, surname: "Hathaway" },
23+
{ name: "John", age: 20, surname: "Johnson"},
24+
];
25+
let ageSortedAnswer = users.sort(byField("age"));
26+
assert.deepEqual(ageSortedKey, ageSortedKey);
27+
});
28+
29+
it("sorts users by surname", function(){
30+
let surnameSortedKey = [
31+
{ name: "Ann", age: 19, surname: "Hathaway" },
32+
{ name: "John", age: 20, surname: "Johnson"},
33+
{ name: "Pete", age: 18, surname: "Peterson" },
34+
];
35+
let surnameSortedAnswer = users.sort(byField("surname"));
36+
assert.deepEqual(surnameSortedAnswer, surnameSortedKey);
37+
});
38+
39+
});
Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1 @@
11

2-
3-
```js run
4-
let users = [
5-
{ name: "John", age: 20, surname: "Johnson" },
6-
{ name: "Pete", age: 18, surname: "Peterson" },
7-
{ name: "Ann", age: 19, surname: "Hathaway" }
8-
];
9-
10-
*!*
11-
function byField(field) {
12-
return (a, b) => a[field] > b[field] ? 1 : -1;
13-
}
14-
*/!*
15-
16-
users.sort(byField('name'));
17-
users.forEach(user => alert(user.name)); // Ann, John, Pete
18-
19-
users.sort(byField('age'));
20-
users.forEach(user => alert(user.name)); // Pete, Ann, John
21-
```
22-

0 commit comments

Comments
 (0)