> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Nullish Coalescing

The `??` (nullish coalescing) operator is a logical operator that evaluates and returns its right-hand operand strictly when its left-hand operand is `null` or `undefined` (collectively termed "nullish"). If the left-hand operand is any other value, the operator returns the left-hand operand.

```typescript theme={"dark"}
const result = leftExpression ?? rightExpression;
```

## Nullish vs. Falsy Evaluation

The primary mechanical distinction of `??` is its strict adherence to nullish values, contrasting with the logical OR (`||`) operator, which triggers on any *falsy* value (e.g., `0`, `""`, `NaN`, `false`). The `??` operator preserves valid falsy primitives that would otherwise be overridden by `||`.

```typescript theme={"dark"}
// ?? evaluates strictly for null or undefined
const val1 = null ?? "fallback";      // "fallback"
const val2 = undefined ?? "fallback"; // "fallback"

// ?? preserves non-nullish falsy values
const val3 = 0 ?? 42;                 // 0
const val4 = "" ?? "default";         // ""
const val5 = false ?? true;           // false

// || overrides all falsy values
const val6 = 0 || 42;                 // 42
const val7 = "" || "default";         // "default"
```

## Short-Circuit Evaluation

The `??` operator implements short-circuiting. The right-hand expression is only evaluated if the left-hand operand resolves to `null` or `undefined`. If the left-hand operand is non-nullish, the right-hand execution is entirely bypassed.

```typescript theme={"dark"}
function computeHeavyOperation(): number {
    // Execution logic
    return 100;
}

const existingValue = 50;
// computeHeavyOperation() is never invoked because existingValue is not nullish
const finalValue = existingValue ?? computeHeavyOperation(); 
```

## Operator Precedence and Chaining

The `??` operator has a lower precedence than `||` and `&&`. To prevent ambiguous evaluation logic, TypeScript and modern JavaScript engines throw a `SyntaxError` if `??` is chained directly with `&&` or `||` without explicit grouping.

```typescript theme={"dark"}
declare const a: boolean | null;
declare const b: boolean;
declare const c: boolean;

// ❌ SyntaxError: Cannot use unparenthesized '??' within '||' and '&&' expressions.
const invalid = a || b ?? c;
const invalid2 = a && b ?? c;

// ✅ Valid: Explicit precedence via parentheses
const valid1 = (a || b) ?? c;
const valid2 = a || (b ?? c);
```

## Type Narrowing

In TypeScript's type system, the `??` operator automatically narrows the inferred type of the resulting expression by excluding `null` and `undefined` from the left operand's type union, provided the right operand does not also include them.

```typescript theme={"dark"}
declare const input: string | null | undefined;

// TypeScript infers 'result' as type 'string'
// It strips null/undefined from 'input' and unions it with the type of "default"
const result = input ?? "default"; 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
