console.log(restaurant.orderPizza?.('onion','tomato','basil') ?? 'Method does not exist');
console.log(restaurant.orderRissotto?.('onion','tomato','basil') ?? 'Method does not exist'
orderPizza and orderRissotto are the two methods inside object restaurant.
When I log them using Nullish Coalesceing operator, the method output gets logged because method is available. However, second part, 'Method does not exist' also gets logged. What might be the reason?
Log:
Your pizza with onion, tomato and basil is ready
Method does not exist
the
Null Safe
returnsnull
, and theNullish coalescing
operator will return the second piece if the first one isnull || undefined
... therefore if your method does not exists, the null safe will return null, and so the second part will be returned, but if the method exists but returnsnull || undefined
value, the first part will be run, but the second part will be printed (since your method returns one of the value thatNullish coalescing
use to determin if the second part should be returned)