nullish coalescing javascript
Old way:
const doge = { name: 'anonystick', height: '50cm', weight: undefined, hasBaby: null, age: 0, babyName: '', babyAge: NaN, } const dogeWeight = doge.weight || 10; // 10 const dogeHasBaby = doge.hasBaby || false; // false const dogeAge = doge.age || 0.1; // 0.1 const dogeBabyName = doge.babyName || 'defaultName'; // 'defaultName' const dogeBabyAge = doge.babyAge || 'defaultAge'; // 'defautlAge'
New way with ES10:
const doge = { name: 'anonystick', height: '50cm', weight: undefined, hasBaby: null, age: 0, babyName: '', babyAge: NaN, } const dogeWeight = doge.weight ?? 10; // 10 const dogeHasBaby = doge.hasBaby ?? false; // false const dogeAge = doge.age ?? 0.1; // 0 const dogeBabyName = doge.babyName ?? 'defaultName'; // '' const dogeBabyAge = doge.babyAge ?? 'defaultAge'; // NaN
Có thể bạn đã miss một số snippets code