Frontend Learning — || vs ?? (Most Developers Get This Wrong) At first glance, both look similar… but they behave very differently in JavaScript. And this small mistake can cause real bugs in production . -> Why this matters: || treats falsy values (0, "", false) as false ?? only checks for null or undefined Using the wrong one can override valid values 💡 Key Takeaway: -> Use ?? when you only want to handle null or undefined A small operator choice can completely change your logic. #JavaScript #FrontendDevelopment #CodingTips #CleanCode #WebDevelopment #InterviewPrep #LearnInPublic #DeveloperJourney
It's not wrong if u know when use it. There are some scenarios that need to use this case. Ex, if u depend on this count to validate value and value cant be zero, so if the count is zero u should add initial value instead. It's not about wrong and correct, It's about when use || and when ??
If you give an example for undefined or null that would really make sense
Rather try this and chill let count = 0; if (count === undefined) { count = 10; } const value = count; console.log(value); // Output: 0 Can also be updated for checking 0 just add this in if condition || count === 0, even for <=0, null.. Never fall for the less words or alphabets count or terms as modern js as shortcut in your programming otherwise all the best.. 😁.. but this isn't what makes your codes short or modern, the concepts and knowing how and where to use them definitely does, for these tiny syntaxes correction you have ai and google etc, and brainstorming will happen anyways, cannot be avoided.
I mean, 10 was expected there But the understanding of what to use between || and ?? in a given situation makes sure that they don't cause an issue
Good one. I’ve seen || override valid values like 0 or empty string in real cases — ?? is much safer for defaults.
This is production bug 😁 I have seen this many places
Simple, visual, and highly effective. Understanding the nuance between these two is a total game-changer for writing predictable code. Thanks for sharing! 🚀
I’ve seen this bug in production more than once — especially when 0, false, or empty strings are valid values. One thing I usually keep in mind: || → fallback for falsy ?? → fallback for missing That small distinction makes a big difference in real apps.