Skip to content

Commit 3fa4c32

Browse files
committed
minor fixes
1 parent 19bf2d3 commit 3fa4c32

File tree

7 files changed

+32
-23
lines changed

7 files changed

+32
-23
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ true + false = 1
99
"$" + 4 + 5 = "$45"
1010
"4" - 2 = 2
1111
"4px" - 2 = NaN
12-
7 / 0 = Infinity
1312
" -9 " + 5 = " -9 5" // (3)
1413
" -9 " - 5 = -14 // (4)
1514
null + 1 = 1 // (5)

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

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ true + false
1616
"$" + 4 + 5
1717
"4" - 2
1818
"4px" - 2
19-
7 / 0
2019
" -9 " + 5
2120
" -9 " - 5
2221
null + 1

1-js/02-first-steps/11-logical-operators/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Logical operators: ||, && and !
1+
# Logical operators
22

3-
There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), '??' (Nullish Coalescing).
3+
There are four logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT), `??` (Nullish Coalescing). Here we cover the first three, the `??` operator is in the next article.
44

55
Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type.
66

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
[recent browser="new"]
44

5-
Here, in this article, we'll say that an expression is "defined" when it's neither `null` nor `undefined`.
6-
75
The nullish coalescing operator is written as two question marks `??`.
86

7+
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. We'll say that an expression is "defined" when it's neither `null` nor `undefined`.
8+
99
The result of `a ?? b` is:
1010
- if `a` is defined, then `a`,
1111
- if `a` isn't defined, then `b`.
1212

13-
1413
In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one.
1514

1615
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
@@ -21,29 +20,31 @@ We can rewrite `result = a ?? b` using the operators that we already know, like
2120
result = (a !== null && a !== undefined) ? a : b;
2221
```
2322

23+
Now it should be absolutely clear what `??` does. Let's see where it helps.
24+
2425
The common use case for `??` is to provide a default value for a potentially undefined variable.
2526

26-
For example, here we show `Anonymous` if `user` isn't defined:
27+
For example, here we show `user` if defined, otherwise `Anonymous`:
2728

2829
```js run
2930
let user;
3031

31-
alert(user ?? "Anonymous"); // Anonymous
32+
alert(user ?? "Anonymous"); // Anonymous (user not defined)
3233
```
3334
34-
Of course, if `user` had any value except `null/undefined`, then we would see it instead:
35+
Here's the example with `user` assigned to a name:
3536
3637
```js run
3738
let user = "John";
3839

39-
alert(user ?? "Anonymous"); // John
40+
alert(user ?? "Anonymous"); // John (user defined)
4041
```
4142
4243
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
4344
44-
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be undefined, if the user decided not to enter a value.
45+
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to enter a value.
4546
46-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are undefined.
47+
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined.
4748
4849
Let's use the `??` operator for that:
4950
@@ -75,7 +76,7 @@ alert(firstName || lastName || nickName || "Anonymous"); // Supercoder
7576
*/!*
7677
```
7778
78-
The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
79+
Historically, the OR `||` operator was there first. It exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
7980
8081
On the other hand, the nullish coalescing operator `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`.
8182
@@ -96,16 +97,18 @@ alert(height || 100); // 100
9697
alert(height ?? 100); // 0
9798
```
9899
99-
- The `height || 100` checks `height` for being a falsy value, and it really is.
100-
- so the result is the second argument, `100`.
100+
- The `height || 100` checks `height` for being a falsy value, and it's `0`, falsy indeed.
101+
- so the result of `||` is the second argument, `100`.
101102
- The `height ?? 100` checks `height` for being `null/undefined`, and it's not,
102103
- so the result is `height` "as is", that is `0`.
103104
104-
If the zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing.
105+
In practice, the zero height is often a valid value, that shouldn't be replaced with the default. So `??` does just the right thing.
105106
106107
## Precedence
107108
108-
The precedence of the `??` operator is rather low: `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table). So `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
109+
The precedence of the `??` operator is about the same as `||`, just a bit lower. It equals `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), while `||` is `6`.
110+
111+
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
109112
110113
So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses:
111114
@@ -139,7 +142,7 @@ The code below triggers a syntax error:
139142
let x = 1 && 2 ?? 3; // Syntax error
140143
```
141144
142-
The limitation is surely debatable, but it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch to `??` from `||`.
145+
The limitation is surely debatable, it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch from `||` to `??`.
143146
144147
Use explicit parentheses to work around it:
145148

1-js/04-object-basics/02-object-copy/article.md

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ let clone = Object.assign({}, user);
186186
187187
It copies all properties of `user` into the empty object and returns it.
188188
189+
There are also other methods of cloning an object, e.g. using the [spread operator](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial.
190+
189191
## Nested cloning
190192
191193
Until now we assumed that all properties of `user` are primitive. But properties can be references to other objects. What to do with them?

1-js/04-object-basics/07-optional-chaining/article.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ userGuest.admin?.(); // nothing (no such method)
166166
*/!*
167167
```
168168
169-
Here, in both lines we first use the dot (`user1.admin`) to get `admin` property, because the user object must exist, so it's safe read from it.
169+
Here, in both lines we first use the dot (`userAdmin.admin`) to get `admin` property, because we assume that the user object exists, so it's safe read from it.
170170
171-
Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `user1`). Otherwise (for `user2`) the evaluation stops without errors.
171+
Then `?.()` checks the left part: if the admin function exists, then it runs (that's so for `userAdmin`). Otherwise (for `userGuest`) the evaluation stops without errors.
172172
173173
The `?.[]` syntax also works, if we'd like to use brackets `[]` to access properties instead of dot `.`. Similar to previous cases, it allows to safely read a property from an object that may not exist.
174174

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,19 @@ But there's a subtle difference between `Array.from(obj)` and `[...obj]`:
225225
So, for the task of turning something into an array, `Array.from` tends to be more universal.
226226
227227
228-
## Get a new copy of an array/object
228+
## Copy an array/object
229229
230230
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
234234
```js run
235235
let arr = [1, 2, 3];
236+
237+
*!*
236238
let arrCopy = [...arr]; // spread the array into a list of parameters
237239
// then put the result into a new array
240+
*/!*
238241
239242
// do the arrays have the same contents?
240243
alert(JSON.stringify(arr) === JSON.stringify(arrCopy)); // true
@@ -252,8 +255,11 @@ Note that it is possible to do the same thing to make a copy of an object:
252255
253256
```js run
254257
let obj = { a: 1, b: 2, c: 3 };
258+
259+
*!*
255260
let objCopy = { ...obj }; // spread the object into a list of parameters
256261
// then return the result in a new object
262+
*/!*
257263
258264
// do the objects have the same contents?
259265
alert(JSON.stringify(obj) === JSON.stringify(objCopy)); // true
@@ -267,7 +273,7 @@ alert(JSON.stringify(obj)); // {"a":1,"b":2,"c":3,"d":4}
267273
alert(JSON.stringify(objCopy)); // {"a":1,"b":2,"c":3}
268274
```
269275
270-
This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj);` or for an array `let arrCopy = Object.assign([], arr);` so we prefer to use it whenever we can.
276+
This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj)` or for an array `let arrCopy = Object.assign([], arr)` so we prefer to use it whenever we can.
271277
272278
273279
## Summary

0 commit comments

Comments
 (0)