Skip to content

Commit ecfd113

Browse files
committed
minor
1 parent f489145 commit ecfd113

File tree

1 file changed

+7
-9
lines changed
  • 1-js/02-first-steps/12-nullish-coalescing-operator

1 file changed

+7
-9
lines changed

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The result of `a ?? b` is:
1111
- if `a` isn't defined, then `b`.
1212

1313

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.
1515

1616
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
1717

@@ -39,7 +39,7 @@ let user = "John";
3939
alert(user ?? "Anonymous"); // John
4040
```
4141
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`.
4343
4444
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.
4545
@@ -77,9 +77,9 @@ alert(firstName || lastName || nickName || "Anonymous"); // Supercoder
7777
7878
The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
7979
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 `||`.
8181
82-
The subtle, yet important difference is that:
82+
The important difference between them is that:
8383
- `||` returns the first *truthy* value.
8484
- `??` returns the first *defined* value.
8585
@@ -96,14 +96,12 @@ alert(height || 100); // 100
9696
alert(height ?? 100); // 0
9797
```
9898
99-
Here, we have a zero height.
100-
10199
- The `height || 100` checks `height` for being a falsy value, and it really is.
102100
- so the result is the second argument, `100`.
103101
- The `height ?? 100` checks `height` for being `null/undefined`, and it's not,
104102
- so the result is `height` "as is", that is `0`.
105103
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.
107105
108106
## Precedence
109107
@@ -155,7 +153,7 @@ alert(x); // 2
155153
156154
## Summary
157155
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.
159157
160158
It's used to assign default values to variables:
161159
@@ -164,5 +162,5 @@ alert(x); // 2
164162
height = height ?? 100;
165163
```
166164
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.
168166
- It's forbidden to use it with `||` or `&&` without explicit parentheses.

0 commit comments

Comments
 (0)