Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 291b5c0

Browse files
committed
minor fixes
1 parent c714756 commit 291b5c0

File tree

1 file changed

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

1 file changed

+7
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
The nullish coalescing operator is written as two question marks `??`.
66

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`.
7+
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. For brevity, we'll say that a value is "defined" when it's neither `null` nor `undefined`.
88

99
The result of `a ?? b` is:
1010
- if `a` is defined, then `a`,
@@ -22,9 +22,9 @@ result = (a !== null && a !== undefined) ? a : b;
2222

2323
Now it should be absolutely clear what `??` does. Let's see where it helps.
2424

25-
The common use case for `??` is to provide a default value for a potentially undefined variable.
25+
The common use case for `??` is to provide a default value.
2626

27-
For example, here we show `user` if defined, otherwise `Anonymous`:
27+
For example, here we show `user` if its value isn't `null/undefined`, otherwise `Anonymous`:
2828

2929
```js run
3030
let user;
@@ -42,9 +42,9 @@ alert(user ?? "Anonymous"); // John (user defined)
4242
4343
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
4444
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.
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 fill in the corresponding values.
4646
47-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined.
47+
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are `null/undefined`.
4848
4949
Let's use the `??` operator for that:
5050
@@ -110,7 +110,7 @@ The precedence of the `??` operator is the same as `||`. They both equal `4` in
110110
111111
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
112112
113-
So if we'd like to choose a value with `??`in an expression with other operators, consider adding parentheses:
113+
So we may need to add parentheses in expressions like this:
114114
115115
```js run
116116
let height = null;
@@ -128,7 +128,7 @@ Otherwise, if we omit parentheses, then as `*` has the higher precedence than `?
128128
// without parentheses
129129
let area = height ?? 100 * width ?? 50;
130130

131-
// ...works the same as this (probably not what we want):
131+
// ...works this way (not what we want):
132132
let area = height ?? (100 * width) ?? 50;
133133
```
134134

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /