DEV Community

Seyed Hamid Mirghafouri
Seyed Hamid Mirghafouri

Posted on

JavaScript || (Logical OR) vs ?? (Nullish Coalescing)

?? is falsy if the variable is null or undefined
|| is falsy if the variable is empty, zero, false, null or undefined

console.log(120 || "falsy") // 120
console.log(0  || "falsy") // "falsy"

console.log("truly" || "falsy") // "truly"
console.log(""     || "falsy") // "falsy"

console.log(true  || "falsy") // true
console.log(false || "falsy") // "falsy"

console.log(undefined || "falsy") // "falsy"
console.log(null      || "falsy") // "falsy"
Enter fullscreen mode Exit fullscreen mode
console.log(120 ?? "falsy") // 120
console.log(0  ?? "falsy") // 0

console.log("truly" ?? "falsy") // "truly"
console.log(""     ?? "falsy") // ""

console.log(true  ?? "falsy") // true
console.log(false ?? "falsy") // false

console.log(undefined ?? "falsy") // "falsy"
console.log(null      ?? "falsy") // "falsy"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay