Join our Newsletter — 33% off our NHI Course

How should React developers prevent conditional rendering from showing unintended values like 0 or NaN?

Use an explicit boolean or a ternary expression when rendering conditionally in JSX. Expressions such as count && can leak non-boolean falsy values into the UI, so React may render 0 or NaN instead of nothing. A safer pattern is count > 0 && … or count ? … : null, which makes the intended output unambiguous.

Why JSX conditionals should stay explicitly boolean

React only treats false, null, and undefined as “render nothing” values. Other falsy values, including 0 and NaN, are still valid render output, so a short-circuit expression can leak them into the UI when the left-hand side is not strictly boolean. The practical fix is to make the condition explicit so the intent is obvious to both React and future readers.

The common trap is assuming JavaScript truthiness and JSX rendering behave the same way in every case. They do not. When a count, length, or numeric expression participates in the condition, the expression can return a number instead of a boolean, which changes the rendered result. Using a comparison such as count > 0 or a ternary avoids that ambiguity and keeps the output aligned with the intended state.

Where && works, and where it breaks down

Short-circuit rendering is still fine when the left side is already a genuine boolean. The problem begins when the left side is a raw numeric value, a derived calculation, or something that can evaluate to 0 or NaN. In those cases, the expression does not mean “show nothing”, it means “return the left-hand value if it is falsy”, and React will faithfully render that value.

That is why patterns such as items.length && ... can be risky if the empty state matters visually, and why a ternary is often the cleaner choice when the alternative output should be explicit. If the “false” branch should be empty, spell that out with : null. If the condition is numeric, convert it into a real boolean first so the component tree is predictable.

Write conditional UI so the intent survives refactors

conditional rendering bugs often appear later, after a harmless refactor turns a boolean into a count, ratio, or computed value. At that point the JSX still compiles, but the rendered output changes in a subtle way. The safest habit is to treat JSX conditions as presentation logic, not as a place to rely on JavaScript coercion rules.

For React teams, the best pattern is to make the “should render” question separate from the “what value exists” question. A comparison such as count > 0, a named boolean like hasItems, or a ternary with an explicit null branch all preserve that separation. This makes component behavior easier to scan, test, and refactor without accidentally exposing implementation values in the UI.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS and OWASP SAMM set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP ASVS V3 — Web Frontend Security JSX conditional rendering is frontend logic that affects displayed output.
V15 — Secure Coding and Architecture The issue is a code-pattern pitfall that should be eliminated in secure, maintainable React code.
Recommendation — Use explicit boolean conditions to prevent unintended values from reaching the UI. Prefer clear conditional expressions that remain correct after refactors.
OWASP SAMM Secure Architecture This bug is prevented by design choices that standardise safe UI patterns across the codebase.
Recommendation — Adopt a consistent rule for boolean-only JSX conditions in component patterns.

Practitioner Guidance

What to verify: Review every conditional JSX expression that uses && and check whether the left side can ever be a number, computed expression, empty string, or NaN. If yes, convert it to a boolean condition or a ternary before it reaches the DOM.

Common mistake: Treating “falsy” as equivalent to “not rendered”. In JSX, that assumption is only safe for false, null, and undefined, not for every falsy JavaScript value.

What good looks like: The condition reads like a decision, not a value leak. A reviewer should be able to tell immediately whether the component renders because of state, count, or an explicit boolean flag.

Practitioner takeaway: Prefer explicit rendering intent over shorthand coercion, because the safest JSX condition is the one that cannot accidentally turn a data value into visible UI.