Full answer
JSX expressions inside braces can hold any JavaScript value, and React decides what to draw for each type.
Renders nothing: null, undefined, true, false. This is why {condition && <Banner />} and {condition ? <A /> : null} work. The booleans exist to support the conditional pattern.
Renders text: strings and numbers. This includes 0, negative numbers and NaN. An empty string renders an empty text node, so nothing visible appears. BigInt values render as numbers too.
Arrays are rendered item by item. An empty array renders nothing, and nested arrays are flattened. Items in arrays need keys because React tracks them by position and key.
Objects (other than React elements) are invalid. { {} } or {user} throws "Objects are not valid as a React child". Convert them first, for example JSON.stringify(user) or render a field.
Functions are not valid as children either and produce a warning, because it usually means you forgot to call them.
The famous trap is the && operator with numbers. {items.length && <List />} evaluates to 0 when the array is empty, and React renders the text "0" on screen. Use a boolean condition: {items.length > 0 && <List />}, or the ternary form. The same applies to NaN. Strings behave similarly: {name && <p>{name}</p>} renders nothing for an empty string (an empty text), which is fine visually.
Remember that false is also what && returns for a false boolean, so it renders nothing, and that attributes behave differently from children: aria-hidden={false} renders aria-hidden="false", and disabled={false} removes the attribute.
The example renders each value on its own and prints the HTML.
import { renderToStaticMarkup } from 'react-dom/server';
const cases = [
['0', 0],
['empty string', ''],
['false', false],
['true', true],
['null', null],
['undefined', undefined],
['empty array', []],
['array [1, 2]', [1, 2]],
['bigint 5n', 5n],
['items.length && ...', [].length && 'List'],
['items.length > 0 && ...', [].length > 0 && 'List'],
];
for (const [label, value] of cases) {
console.log(label + ' -> "' + renderToStaticMarkup(<p>{value}</p>) + '"');
}
try {
renderToStaticMarkup(<p>{{ name: 'Asha' }}</p>);
} catch (error) {
console.log('plain object -> error: ' + error.message.split('.')[0]);
}This ran as a script with renderToStaticMarkup. Each value is wrapped in a paragraph so the result is visible. NaN was left out of the loop because React logs a development warning when it renders it. A separate run confirmed that NaN renders as the text NaN and triggers that warning. Output of the run: 0 -> "<p>0</p>" empty string -> "<p></p>" false -> "<p></p>" true -> "<p></p>" null -> "<p></p>" undefined -> "<p></p>" empty array -> "<p></p>" array [1, 2] -> "<p>12</p>" bigint 5n -> "<p>5</p>" items.length && ... -> "<p>0</p>" items.length > 0 && ... -> "<p></p>" plain object -> error: Objects are not valid as a React child (found: object with keys {name})
Key points
- null, undefined, true and false render nothing.
- Strings and numbers render as text, including 0 and NaN. BigInt renders as text.
- Arrays render item by item, and empty arrays render nothing.
- Plain objects throw "Objects are not valid as a React child".
- Use count > 0 instead of count && ... to avoid rendering a stray 0.
Common mistakes
- Writing {items.length && <List />} and rendering 0.
- Rendering an object directly in JSX.
- Expecting false to print the word false.
- Forgetting that attributes handle false differently from children.
- Using index-based keys for arrays that change.
Follow-ups
- Why does React treat booleans as "render nothing"?
- How do you render a Date or an object safely?
- How does disabled={false} differ from aria-hidden={false}?
Tips
- Lead with the 0 trap. It is the part interviewers ask about.
- Give the corrected expression with > 0.
- Mention that objects throw while arrays render.