---
title: "Nullish Coalescing vs Logical OR"
description: "Understanding the difference between ?? and || operators in JavaScript. Practical notes and code examples on Nullish in JS."
url: "https://h3manth.com/scribe/nullish-coalescing-vs-logical-or/"
canonical_url: "https://h3manth.com/scribe/nullish-coalescing-vs-logical-or/"
markdown_url: "https://h3manth.com/scribe/blog/nullish-coalescing-vs-logical-or.md"
publishDate: "2020-09-03T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","es2020","tc39"]
word_count: 189
reading_time_minutes: 1
x_markdown_tokens: 362
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Nullish Coalescing vs Logical OR

> Understanding the difference between ?? and || operators in JavaScript. Practical notes and code examples on Nullish in JS.

Whenever I talk about `??` people have asked me the difference between Logical OR (`||`) vs Nullish coalescing (`??`) operators, so here is a quick explanation.

## The Key Difference

- `||` returns the right-hand side if left-hand side is **falsy** (false, 0, '', null, undefined, NaN)
- `??` returns the right-hand side only if left-hand side is **nullish** (null or undefined)

## Examples

```javascript
// With ||
0 || 'default'      // 'default' (0 is falsy)
'' || 'default'     // 'default' (empty string is falsy)
false || 'default'  // 'default' (false is falsy)
null || 'default'   // 'default'
undefined || 'default' // 'default'

// With ??
0 ?? 'default'      // 0 (0 is not nullish)
'' ?? 'default'     // '' (empty string is not nullish)
false ?? 'default'  // false (false is not nullish)
null ?? 'default'   // 'default'
undefined ?? 'default' // 'default'
```

## When to Use Which

Use `??` when:
- You want to allow `0`, `''`, or `false` as valid values
- You only want to provide a default for `null` or `undefined`

Use `||` when:
- You want a default for any falsy value
