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: 1-js/02-first-steps/09-comparison/article.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,9 @@ The algorithm to compare two strings is simple:
57
57
4. Repeat until the end of either string.
58
58
5. If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
59
59
60
-
In the examples above, the comparison `'Z' > 'A'` gets to a result at the first step while the strings `'Glow'` and `'Glee'` are compared character-by-character:
60
+
In the first example above, the comparison `'Z' > 'A'` gets to a result at the first step.
61
+
62
+
The second comparison `'Glow'` and `'Glee'` needs more steps as strings are compared character-by-character:
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-nullish-coalescing-operator/article.md
+7-9
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ The result of `a ?? b` is:
11
11
- if `a` isn't defined, then `b`.
12
12
13
13
14
-
In other words, `??` returns the first argument if it's defined. Otherwise, the second one.
14
+
In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one.
15
15
16
16
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
17
17
@@ -39,7 +39,7 @@ let user = "John";
39
39
alert(user ??"Anonymous"); // John
40
40
```
41
41
42
-
We can also use a sequence of `??` to select the first defined value from a list.
42
+
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
43
43
44
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.
The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
79
79
80
-
On the other hand, the nullish coalescing operator `??` was added only recently, and the reason for that was that people weren't quite happy with `||`.
80
+
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 `||`.
81
81
82
-
The subtle, yet important difference is that:
82
+
The important difference between them is that:
83
83
- `||` returns the first *truthy* value.
84
84
- `??` returns the first *defined* value.
85
85
@@ -96,14 +96,12 @@ alert(height || 100); // 100
96
96
alert(height ??100); // 0
97
97
```
98
98
99
-
Here, we have a zero height.
100
-
101
99
- The `height ||100` checks `height` for being a falsy value, and it really is.
102
100
- so the result is the second argument, `100`.
103
101
- The `height ??100` checks `height` for being `null/undefined`, and it's not,
104
102
- so the result is `height` "as is", that is `0`.
105
103
106
-
If we assume that zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing.
104
+
If the zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing.
107
105
108
106
## Precedence
109
107
@@ -155,7 +153,7 @@ alert(x); // 2
155
153
156
154
## Summary
157
155
158
-
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
156
+
- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list.
159
157
160
158
It's used to assign default values to variables:
161
159
@@ -164,5 +162,5 @@ alert(x); // 2
164
162
height = height ??100;
165
163
```
166
164
167
-
- The operator `??` has a very low precedence, a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression.
165
+
- The operator `??` has a very low precedence, only a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression.
168
166
- It's forbidden to use it with `||` or `&&` without explicit parentheses.
0 commit comments