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
Please note: the `?.` syntax works exactly where it's placed, not any further.
62
+
Please note: the `?.` syntax makes optional the value before it, but not any further.
63
+
64
+
In the example above, `user?.` allows only `user` to be `null/undefined`.
66
65
67
-
In the last two lines the evaluation stops immediately after `user?.`, never accessing further properties. But if the `user` actually exists, then the further intermediate properties, such as `user.address` must exist.
66
+
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.
68
67
69
68
```warn header="Don't overuse the optional chaining"
70
69
We should use `?.` only where it's ok that something doesn't exist.
@@ -74,14 +73,14 @@ For example, if according to our coding logic `user` object must be there, but `
74
73
So, if`user` happens to be undefined due to a mistake, we'll know about it and fix it. Otherwise, coding errors can be silenced where not appropriate, and become more difficult to debug.
75
74
```
76
75
77
-
````warn header="The variable before `?.` must exist"
78
-
If there's no variable `user`, then `user?.anything` triggers an error:
76
+
````warn header="The variable before `?.` must be declared"
77
+
If there's no variable `user` at all, then `user?.anything` triggers an error:
79
78
80
79
```js run
81
80
// ReferenceError: user is not defined
82
81
user?.address;
83
82
```
84
-
The optional chaining only tests for`null/undefined`, doesn't interfere with any other language mechanics.
83
+
There must be `let/const/var user`. The optional chaining works only for declared variables.
0 commit comments