1---
2title: Use Explicit Conditional Rendering
3impact: LOW
4impactDescription: prevents rendering 0 or NaN
5tags: rendering, conditional, jsx, falsy-values
6---
7
8## Use Explicit Conditional Rendering
9
10Use explicit ternary operators (`? :`) instead of `&&` for conditional rendering when the condition can be `0`, `NaN`, or other falsy values that render.
11
12**Incorrect (renders "0" when count is 0):**
13
14```tsx
15function Badge({ count }: { count: number }) {
16 return (
17 <div>
18 {count && <span className="badge">{count}</span>}
19 </div>
20 )
21}
22
23// When count = 0, renders: <div>0</div>
24// When count = 5, renders: <div><span class="badge">5</span></div>
25```
26
27**Correct (renders nothing when count is 0):**
28
29```tsx
30function Badge({ count }: { count: number }) {
31 return (
32 <div>
33 {count > 0 ? <span className="badge">{count}</span> : null}
34 </div>
35 )
36}
37
38// When count = 0, renders: <div></div>
39// When count = 5, renders: <div><span class="badge">5</span></div>
40```