Basics

What is short-circuit evaluation — and where that stray «0» on screen came from

Illustration: a fork with two doors — the first is shut, and nobody even reaches the second

Your task list is empty. You expect a blank screen. Instead, a lonely 0 sits in the corner. You didn't write it. AI didn't write it either. It just showed up.

The culprit is one line AI puts into React code all the time: {tasks.length && <TaskList />}. It almost always works. Except when the list is empty. Let's see why — it's called short-circuit evaluation, and it explains a couple more mysteries too.

What is short-circuit evaluation

Short-circuit evaluation means JavaScript evaluates a condition left to right and stops as soon as the answer is known. It doesn't even look at the right side.

It works with two operators:

  • a && b — "and". If a is falsy, the answer is already clear: "no". It never gets to b.
  • a || b — "or". If a is truthy, the answer is already clear: "yes". b gets skipped too.

Like real life. "If there's bread and butter at home, I'll make a sandwich." You open the bread box — empty. You don't bother checking the fridge for butter.

The big surprise: the result isn't true or false

Here's where beginners trip. && and || don't return true or false. They return one of their own values — the one they stopped on.

0 && 'hello'        // 0
5 && 'hello'        // 'hello'
'' || 'guest'       // 'guest'
'Anna' || 'guest'   // 'Anna'

&& returns the first falsy value, or the last one if none are falsy. || returns the first truthy value, or the last one if none are truthy.

What counts as falsy — 0, an empty string, null, undefined, NaN and false — is covered in detail in what is a boolean.

Solving the zero

Back to the screen. tasks.length && <TaskList />:

  • 3 tasks → 3 is truthy → && moves on and returns <TaskList />. All good.
  • 0 tasks → 0 is falsy → && stops and returns the 0 itself.

And React can render numbers. It silently skips false, null and undefined, but 0 it dutifully puts on screen. There's your orphan in the corner.

In React Native it's worse: a bare 0 outside a <Text> component crashes the app with "Text strings must be rendered within a <Text> component".

Fix it one of three ways:

{tasks.length > 0 && <TaskList />}     // comparison gives true/false
{!!tasks.length && <TaskList />}       // !! turns it into a boolean
{tasks.length ? <TaskList /> : null}   // ternary operator

The React docs say it outright: don't put numbers on the left side of &&.

Trap two: || eats the zero

The reverse story with ||. It's often used for a default value:

const volume = settings.volume || 50

The user turned the volume down to zero. 0 is falsy → || moves on → volume is 50 again. The person muted the sound, and the app turned it back on.

That's what the ?? operator is for. It falls back only when the left side is null or undefined — meaning there really is no value. It leaves zero and empty strings alone:

const volume = settings.volume ?? 50   // 0 stays 0

More on "empty" values in what is null.

Short-circuiting as a trick

There's a useful side too. Since the right part doesn't run, people use it as a short if:

isValid && save()          // save only if the form is valid
user && console.log(user.name)

It works, but reads worse than a plain if. A good rule: use if for actions, and &&, ||, ?? for picking a value. AI code that hides all its logic in a chain like a && b() || c() is worth asking to rewrite.

How to use this

  • See a stray 0 in your UI — look for && with a number or .length on its left.
  • Default values — ?? if 0 and empty strings are valid. || only if those should be replaced too.
  • Asking AI for conditional rendering — add: "no numbers on the left of &&, use a comparison or a ternary".

Is this only in JavaScript?

No, it's in almost every language. In Python it's and and or, and they also return one of the values: 0 or 'guest' gives 'guest'.

Why doesn't the bug show up right away?

Because as long as the list has items, everything works. The zero only appears in the empty state — and that's the state people test least during development.

Does the order of && and || matter?

Yes. JavaScript goes strictly left to right. Put first whatever is cheaper to check or guards against an error: user && user.name — first make sure user exists.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →