(JavaScript) || and && short circuiting / Nullish
// Short Circuiting (&& and ||)
'use strict';
// Use ANY data type, return ANY data type,
// short-circuiting
// If the first value is truthy value, then immediately return that value
// OR circuiting returns the first truthy value of all the operands or the last value if all the operands are falsy
// AND operand will return first falsy value or the last value if all the operands are truthy
// Usually use OR operator to set a default value then use AND operand to execute the second operand
// If the first one is truthy one
console.log('---------------------------------------OR-----------------------------------');
console.log(3 || 'Hongsik'); // 3
console.log('' || 'Hongsik'); // Hongsik
console.log(true || 0); // true
console.log(undefined || null); // null
console.log(undefined || 0 || '' || 'Hello' || 23 || null); // Hello
const guests1 = restaurant.numGuests ? restaurant.numGuests : 10;
console.log(guests1);
const guests2 = restaurant.numGuests || 10; // Set default value
console.log(guests2);
console.log('---------------------------------------AND-----------------------------------');
console.log(0 && 'Hongsik');
console.log(7 && 'Hongsik');
console.log('Hello' && 23 && null && 'jonas'); // NULL
if (restaurant.orderPizza) {
restaurant.orderPizza('mushrooms', 'spinach');
}
restaurant.orderPizza && restaurant.orderPizza('mushrooms', 'spinach'); // If orederPizza funtion exists in restaurant,
// then, call orderPizza method. && circuiting
/// Nullish: null and undefined (NOT 0 or '')
const guestCorrect = restaurant.numGuess ?? 10;
console.log(guestCorrect);