JavaScript OR vs Nullish Coalescing Explained

🚀 Day 18 - Poll answer & Explanation || vs ?? — Small difference, big impact ⚡ const a = 0; const b = 0; console.log(a || 'default');  // 'default' console.log(a ?? 'default');  // 0 const config = { timeout: 0 }; console.log(config.timeout || 5000); // 5000 console.log(config.timeout ?? 5000); // 0 Explanation: || (OR operator): - Returns the first "truthy" value - 0 is falsy → so it picks 'default' or 5000 ?? (Nullish Coalescing): - Returns the right side ONLY if value is null or undefined - 0 is NOT null/undefined → so it keeps 0 Key Difference: || treats 0, "", false as falsy ?? treats only null and undefined as empty Use ?? when 0 or false are valid values #JavaScript #WebDevelopment #Frontend #Coding #100DaysOfCode #JSConcepts #LearnToCode #Programming #Developer #CodeNewbie

To view or add a comment, sign in

Explore content categories