The optional chaining 5 is a safe way to access nested object properties, even if an intermediate property doesn’t exist. Show 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. As an example, let’s say we have 6 objects that hold the information about our users.Most of our users have addresses in 7 property, with the street 8, but some did not provide them.In such case, when we attempt to get 8, and the user happens to be without an address, we get an error:
That’s the expected result. JavaScript works like this. As 7 is 1, an attempt to get 8 fails with an error.In many practical cases we’d prefer to get 1 instead of an error here (meaning “no street”).…and another example. In Web development, we can get an object that corresponds to a web page element using a special method call, such as 4, and it returns 5 when there’s no such element.
Once again, if the element doesn’t exist, we’ll get an error accessing 6 property of 5. And in some cases, when the absence of the element is normal, we’d like to avoid the error and just accept 8 as the result.How can we do this? The obvious solution would be to check the value using 9 or the conditional operator 0, before accessing its property, like this:
It works, there’s no error… But it’s quite inelegant. As you can see, the 1 appears twice in the code.Here’s how the same would look for 2:
We can see that the element search 4 is actually called twice here. Not good.For more deeply nested properties, it becomes even uglier, as more repetitions are required. E.g. let’s get 4 in a similar fashion.
That’s just awful, one may even have problems understanding such code. There’s a little better way to write it, using the 5 operator:
AND’ing the whole path to the property ensures that all components exist (if not, the evaluation stops), but also isn’t ideal. As you can see, property names are still duplicated in the code. E.g. in the code above, 7 appears three times.That’s why the optional chaining 5 was added to the language. To solve this problem once and for all!The optional chaining 5 stops the evaluation if the value before 5 is 1 or 5 and returns 1.Further in this article, for brevity, we’ll be saying that something “exists” if it’s not 5 and not 1.In other words, 5:
Here’s the safe way to access 8 using 5:
The code is short and clean, there’s no duplication at all. Here’s an example with 2:
Reading the address with 4 works even if 6 object doesn’t exist:
Please note: the 5 syntax makes optional the value before it, but not any further.E.g. in 7 the 5 allows 6 to safely be 0 (and returns 1 in that case), but that’s only for 6. Further properties are accessed in a regular way. If we want some of them to be optional, then we’ll need to replace more 3 with 5.Don’t overuse the optional chaining We should use 5 only where it’s ok that something doesn’t exist.For example, if according to our code logic 6 object must exist, but 7 is optional, then we should write 8, but not 9.Then, if 6 happens to be undefined, we’ll see a programming error about it and fix it. Otherwise, if we overuse 5, coding errors can be silenced where not appropriate, and become more difficult to debug.The variable before If there’s no variable 6 at all, then 4 triggers an error:
The variable must be declared (e.g. 5 or as a function parameter). The optional chaining works only for declared variables.As it was said before, the 5 immediately stops (“short-circuits”) the evaluation if the left part doesn’t exist.So, if there are any further function calls or operations to the right of 5, they won’t be made.For instance: 0The optional chaining 5 is not an operator, but a special syntax construct, that also works with functions and square brackets.For example, 9 is used to call a function that may not exist.In the code below, some of our users have 0 method, and some don’t: 1Here, in both lines we first use the dot ( 1) to get 0 property, because we assume that the 6 object exists, so it’s safe read from it.Then 9 checks the left part: if the 0 function exists, then it runs (that’s so for 6). Otherwise (for 7) the evaluation stops without errors.The 8 syntax also works, if we’d like to use brackets 9 to access properties instead of dot 3. Similar to previous cases, it allows to safely read a property from an object that may not exist. 2Also we can use 5 with 2: 3We can use The optional chaining 5 has no use on the left side of an assignment.For example: 4The optional chaining 5 syntax has three forms:
As we can see, all of them are straightforward and simple to use. The 5 checks the left part for 0 and allows the evaluation to proceed if it’s not so.A chain of 5 allows to safely access nested properties.Still, we should apply 5 carefully, only where it’s acceptable, according to our code logic, that the left part doesn’t exist. So that it won’t hide programming errors from us, if they occur. |