Member-only story
Javascript The “optional chaining” operator
Discover how the “optional chaining” operator that comes with the new ES2020 proposal works
One of the features that the ECMAScript 2020 specification brings is a new operator: the optional chaining, present in other languages such as Swift and that comes to Javascript to make it easier for us to work with objects whose keys we are not sure that exist.
In order to summarize the characteristics of this new operator, I have prepared this article that I hope will serve you because it is a very useful feature as soon as we get used to using it.
Let’s go there!
Motivation of the “optional chaining operator”
The reason for being this operator, as I mentioned at the beginning of the article, is to allow us to work with objects of various dimensions without worrying about whether, on the way, we come to a property that is not defined.
For example, suppose we have the following object:
const person = {
name: 'Gerardo',
email: 'mail@mail.com'
}
If we try to do the following:
const jobTitle = person.currentJob.title