Skip to content

Commit d6e8864

Browse files
committed
minor fixes
1 parent 10d1b1f commit d6e8864

File tree

1 file changed

+45
-15
lines changed
  • 1-js/04-object-basics/07-optional-chaining

1 file changed

+45
-15
lines changed

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

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,81 @@ The optional chaining `?.` is a safe way to access nested object properties, eve
99

1010
If you've just started to read the tutorial and learn JavaScript, maybe the problem hasn't touched you yet, but it's quite common.
1111

12-
As an example, let's consider objects for user data. Most of our users have addresses in `user.address` property, with the street `user.address.street`, but some did not provide them.
12+
As an example, consider objects for user data. Most of our users have addresses in `user.address` property, with the street `user.address.street`, but some did not provide them.
1313

14-
In such case, when we attempt to get `user.address.street`, we'll get an error:
14+
In such case, when we attempt to get `user.address.street`, we may get an error:
1515

1616
```js run
17-
let user = {}; // the user without "address" property
17+
let user = {}; // a user without "address" property
1818

1919
alert(user.address.street); // Error!
2020
```
2121

22-
That's the expected result, JavaScript works like this, but many practical cases we'd prefer to get `undefined` instead of an error (meaning "no street").
22+
That's the expected result, JavaScript works like this. As `user.address` is `undefined`, the attempt to get `user.address.street` fails with an error. Although, in many practical cases we'd prefer to get `undefined` instead of an error here (meaning "no street").
2323

24-
...And another example. In the web development, we may need to get an information about an element on the page, that sometimes doesn't exist:
24+
...And another example. In the web development, we may need the information about an element on the page. The element is returned by `document.querySelector('.elem')`, and the catch is again - that it sometimes doesn't exist:
2525

2626
```js run
27-
// Error if the result of querySelector(...) is null
28-
let html = document.querySelector('.my-element').innerHTML;
27+
// the result of the call document.querySelector('.elem') may be an object or null
28+
let html = document.querySelector('.elem').innerHTML; // error if it's null
2929
```
3030

31-
Before `?.` appeared in the language, the `&&` operator was used to work around that.
31+
Once again, we may want to avoid the error in such case.
3232

33-
For example:
33+
How can we do this?
34+
35+
The obvious solution would be to check the value using `if` or the conditional operator `?`, before accessing it, like this:
36+
37+
```js
38+
let user = {};
39+
40+
alert(user.address ? user.address.street : undefined);
41+
```
42+
43+
...But that's quite inelegant. As you can see, the `user.address` is duplicated in the code. For more deeply nested properties, that becomes a problem.
44+
45+
E.g. let's try getting `user.address.street.name`.
46+
47+
We need to check both `user.address` and `user.address.street`:
48+
49+
```js
50+
let user = {}; // user has no address
51+
52+
alert(user.address ? user.address.street ? user.address.street.name : null : null);
53+
```
54+
55+
That looks awful.
56+
57+
Before the optional chaining `?.` was added to the language, people used the `&&` operator for such cases:
3458

3559
```js run
3660
let user = {}; // user has no address
3761

38-
alert( user && user.address && user.address.street ); // undefined (no error)
62+
alert( user.address && user.address.street && user.address.street.name ); // undefined (no error)
3963
```
4064

41-
AND'ing the whole path to the property ensures that all components exist (if not, the evaluation stops), but is cumbersome to write.
65+
AND'ing the whole path to the property ensures that all components exist (if not, the evaluation stops), but also isn't ideal.
66+
67+
As you can see, the property names are still duplicated in the code. E.g. in the code above, `user.address` appears three times.
68+
69+
And now, finally, the optional chaining comes to the rescue!
4270

4371
## Optional chaining
4472

4573
The optional chaining `?.` stops the evaluation and returns `undefined` if the part before `?.` is `undefined` or `null`.
4674

4775
**Further in this article, for brevity, we'll be saying that something "exists" if it's not `null` and not `undefined`.**
4876

49-
Here's the safe way to access `user.address.street`:
77+
Here's the safe way to access `user.address.street` using `?.`:
5078

5179
```js run
5280
let user = {}; // user has no address
5381

5482
alert( user?.address?.street ); // undefined (no error)
5583
```
5684
85+
The code is short and clean, there's no duplication at all.
86+
5787
Reading the address with `user?.address` works even if `user` object doesn't exist:
5888
5989
```js run
@@ -65,14 +95,14 @@ alert( user?.address.street ); // undefined
6595
6696
Please note: the `?.` syntax makes optional the value before it, but not any further.
6797
68-
In the example above, `user?.` allows only `user` to be `null/undefined`.
98+
In the example above, `user?.address.street` allows only `user` to be `null/undefined`.
6999
70100
On the other hand, if `user` does exist, then it must have `user.address` property, otherwise `user?.address.street` gives an error at the second dot.
71101
72102
```warn header="Don't overuse the optional chaining"
73103
We should use `?.` only where it's ok that something doesn't exist.
74104

75-
For example, if according to our coding logic `user` object must be there, but `address` is optional, then `user.address?.street` would be better.
105+
For example, if according to our coding logic `user` object must exist, but `address` is optional, then we should write `user.address?.street`, but not `user?.address?.street`.
76106

77107
So, if `user` happens to be undefined due to a mistake, we'll see a programming error about it and fix it. Otherwise, coding errors can be silenced where not appropriate, and become more difficult to debug.
78108
```
@@ -84,7 +114,7 @@ If there's no variable `user` at all, then `user?.anything` triggers an error:
84114
// ReferenceError: user is not defined
85115
user?.address;
86116
```
87-
There must be a declaration (e.g. `let/const/var user`). The optional chaining works only for declared variables.
117+
The variable must be declared (e.g. `let/const/var user` or as a function parameter). The optional chaining works only for declared variables.
88118
````
89119

90120
## Short-circuiting

0 commit comments

Comments
 (0)