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.
@@ -75,13 +74,13 @@ So, if `user` happens to be undefined due to a mistake, we'll know about it and
75
74
```
76
75
77
76
````warn header="The variable before `?.` must be declared"
78
-
If there's no variable `user`, then `user?.anything` triggers an error:
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