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.')

本站由 @Eureka 使用 Stellar 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。