Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-181cc781

This commit is contained in:
Ilya Kantor 2020-10-05 05:07:51 +00:00
commit b5cb105843
2 changed files with 10 additions and 10 deletions

View file

@ -57,7 +57,9 @@ The algorithm to compare two strings is simple:
4. Repeat until the end of either string.
5. If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
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:
In the first example above, the comparison `'Z' > 'A'` gets to a result at the first step.
The second comparison `'Glow'` and `'Glee'` needs more steps as strings are compared character-by-character:
1. `G` is the same as `G`.
2. `l` is the same as `l`.

View file

@ -11,7 +11,7 @@ The result of `a ?? b` is:
- if `a` isn't defined, then `b`.
In other words, `??` returns the first argument if it's defined. Otherwise, the second one.
In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one.
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two.
@ -39,7 +39,7 @@ let user = "John";
alert(user ?? "Anonymous"); // John
```
We can also use a sequence of `??` to select the first defined value from a list.
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
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.
@ -77,9 +77,9 @@ alert(firstName || lastName || nickName || "Anonymous"); // Supercoder
The OR `||` operator exists since the beginning of JavaScript, so developers were using it for such purposes for a long time.
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 `||`.
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 `||`.
The subtle, yet important difference is that:
The important difference between them is that:
- `||` returns the first *truthy* value.
- `??` returns the first *defined* value.
@ -96,14 +96,12 @@ alert(height || 100); // 100
alert(height ?? 100); // 0
```
Here, we have a zero height.
- The `height || 100` checks `height` for being a falsy value, and it really is.
- so the result is the second argument, `100`.
- The `height ?? 100` checks `height` for being `null/undefined`, and it's not,
- so the result is `height` "as is", that is `0`.
If we assume that zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing.
If the zero height is a valid value, that shouldn't be replaced with the default, then `??` does just the right thing.
## Precedence
@ -155,7 +153,7 @@ alert(x); // 2
## Summary
- The nullish coalescing operator `??` provides a short way to choose a "defined" value from the list.
- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list.
It's used to assign default values to variables:
@ -164,5 +162,5 @@ alert(x); // 2
height = height ?? 100;
```
- The operator `??` has a very low precedence, a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression.
- The operator `??` has a very low precedence, only a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression.
- It's forbidden to use it with `||` or `&&` without explicit parentheses.