Course contentsShow
← React
Lesson 134 of 2,3964. Conditional Rendering in JSXFree lesson

Falsy Values Rendering: 0, NaN, and Empty Strings

Understand which falsy values render to the DOM and cause bugs with && operators, especially `0` from `.length`.

Falsy Values Rendering: 0, NaN, and Empty Strings

What you'll learn: How different falsy values behave in JSX and which ones cause unexpected bugs when you think they'll render nothing.

The Core Problem

You've learned that null, undefined, true, and false render as nothing in JSX. But not all falsy values disappear. Some actually render visible text to the screen, and this creates sneaky bugs—especially with the && operator.

Which Falsy Values Render?

Here's the critical breakdown:

  • null and undefined: Render nothing ✓
  • true and false: Render nothing ✓
  • 0 (zero): Renders literally as "0" on screen ⚠️
  • NaN: Renders literally as "NaN" on screen ⚠️
  • "" (empty string): Renders nothing ✓

The Classic Bug

This innocent-looking code will display "0" when the array is empty:

{items.length && <div>You have items!</div>}

Why? Because items.length evaluates to 0 when empty. The && operator returns 0, and React renders it. You expected nothing, but got a zero on your page.

The Fix

Always coerce to a proper boolean:

{items.length > 0 && <div>You have items!</div>}
{!!items.length && <div>You have items!</div>}

The NaN Trap

Similarly, math operations can produce NaN, which will appear as text:

{someValue / 0 && <div>Calculated!</div>}
// If someValue is 0, you'll see "NaN" on screen

Key Takeaway: In React, 0 and NaN render as visible text, not as nothing. Always use explicit boolean comparisons with && to avoid accidental rendering of these falsy values.