Nullish coalescing operator VS OR operator

please guys, is there any advantage of || over ?? . I see that ?? solves some of the problems that || can not solve

?? is newer so there’s a higher chance it’s unsupported on your user’s browser.

They function differently, so I don’t think you can really make a direct comparison. If I’m checking that something is undefined or null, I would prefer ?? over ||.

The advantage is that is selects differently. If null and undefined are the only falsy values you care about, you use that.

const count = 0

const response1 = count || undefined
const response2 = count ?? undefined

Is 0 a valid count? The first one thinks it isn’t, the second one thinks it is.

Use || unless ?? is specifically what you want.

Yes, there could be compatibility issues with NCOs, but that is what babel is for.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.