Optional Chaining
March 6th, 2021
A common pattern in JS is to perform a search for an element and then, if you get a result, perform another operation on this item.
If you know you will always get a result from your search, it's possible to chain your operations using a period:
If you do not, to avoid errors, you normally would have to store the result of your search, then check it has a value with an IF statement, then proceed with an operation on it if it exists, something like this:
With the optional chaining syntax (basically just a question mark before the chaining period) it's now possible to perform this check while also chaining making for more concise and readable code.
Thankyou ES6!