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/04-object-basics/07-optional-chaining/article.md
+45-15Lines changed: 45 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -9,51 +9,81 @@ The optional chaining `?.` is a safe way to access nested object properties, eve
9
9
10
10
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.
11
11
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.
13
13
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:
15
15
16
16
```js run
17
-
let user = {}; //the user without "address" property
17
+
let user = {}; //a user without "address" property
18
18
19
19
alert(user.address.street); // Error!
20
20
```
21
21
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").
23
23
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:
25
25
26
26
```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
29
29
```
30
30
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.
32
32
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:
...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`:
Please note: the `?.` syntax makes optional the value before it, but not any further.
67
97
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`.
69
99
70
100
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.
71
101
72
102
```warn header="Don't overuse the optional chaining"
73
103
We should use `?.` only where it's ok that something doesn't exist.
74
104
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`.
76
106
77
107
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.
78
108
```
@@ -84,7 +114,7 @@ If there's no variable `user` at all, then `user?.anything` triggers an error:
84
114
// ReferenceError: user is not defined
85
115
user?.address;
86
116
```
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.
0 commit comments