JavaScript ?? vs ||: Understanding the Difference

In all this AI-assisted coding, it’s easy to forget the fundamentals what things actually do and why we use them. A small one I still see misused: ?? vs || in JavaScript. ?? (nullish coalescing) only falls back when the value is null or undefined. || (logical OR) falls back on any falsy value like 0, false, '', NaN, etc. That difference matters more than it looks: 0 || 10 // 10 0 ?? 10 // 0 If 0 or false are valid values in your logic, || can quietly introduce bugs. ?? won’t. Nothing new here but worth remembering. #javascript #javascriptTips

  • text

Well explained in short and simple way 👏. For those who want to dive deep, can check out this article: https://www.freecodecamp.org/news/nullish-coalescing-operator-in-javascript/

To view or add a comment, sign in

Explore content categories