Optional_Chaining
?
Now if you want to know whether a restaurant’s opening hours in Monday
1 | console.log(restaurant.openingHours.mon); |
If mon is not exist, it will be undefined, not a big deal.
But if openingHours is not exist ,it will be a error.
So we can solve this problem with the ?
1 | console.log(restaurant.openingHours?.mon); |
We can also use it to use method.
1 | console.log(restaurant.order?.(0,1) ?? 'Method does not exist.') |