JavaScript Nullish Coalescing Operator

المعرفة:: JavaScript
الحالة:: ملاحظة_مؤرشفة
المراجع:: The Complete JavaScript Course 2022 From Zero to Expert


  • Introduced in ES2020.
  • It works with the concept of nullish values (null/undefined) instead of fasly values. ^82e081
  • Values of 0, '', or false are NOTnullish values, but they are falsy.
restaurant.numGuests = 0;  
const guests1 = restaurant.numGuests || 10;  
console.log(guests1); // 10  

// Nullish: null and undefined (NOT 0, false or '')  
const guestCorrect = restaurant.numGuests ?? 10;  
console.log(guestCorrect); // 0  

Last update : August 14, 2023
Created : August 23, 2022

Comments

Comments