← Fundamentals
Fundamentals

Conditions and truthiness

The inspection found zero. Is that the same as no inspection?

Make each condition answer the question the decision actually needs.

Outcome

Write conditions that establish the intended fact: completion, presence or an explicit true/false answer.

Terminology (3)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Condition
The expression or conditional pattern deciding whether a branch is selected.
Presence
Whether an optional datum exists, independently of its stored number or boolean.
Truthiness
A language’s rules for treating a value as true or false in a conditional context; some languages require a boolean instead.

Truth, quantity and presence

A condition controls whether a branch runs. A comparison constructs a boolean answer to a specific question, such as whether a count exceeds a limit. Whether other expressions can act as conditions depends on the selected language’s conversion and type rules.

Quantity, presence and approval are separate facts. An optional answer can be present while its contained value is zero or false. Identify which fact a condition establishes before using it to choose an action.

01The question in the condition

A completed inspection can record zero

A build-review panel displays a completed inspection, its reported defect count and the QA and release approvals. Zero defects is a supplied result; an unknown approval and a refusal are different states.

The desk has two facts: whether an inspection happened, and how many items it found. A count of zero is compatible with a completed inspection. Predict which lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const checked = true;
const count = 0;

if (checked) console.log("complete");
if (count > 0) console.log("positive");
Which lines print?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Save one complete example at a time under the shown filename in a fresh folder. Run its shown command. A checker refusal is evidence before execution, not an empty runtime output. The browser lab supplies its own editor and runner.

02Value versus presence

Zero is a value, not a universal absence test

The desk received a real count of zero. This complete example tests the shown native expression. Decide whether it reaches execution before predicting an output.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const count: number | null = 0;

if (count) {
  console.log("recorded");
} else {
  console.log("missing");
}
What happens with the supplied count?

03Unknown versus refused

A present answer can still be false

An approval can be unknown, explicitly false, or explicitly true. This example prints presence first and the contained approval second. The source supplies a present refusal.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const approval: boolean | null = false;

console.log(approval !== null, Boolean(approval));
Which pair prints?

04Practice

Repair the inspection desk

Keep one small inspection program through three repairs. The supplied reader decodes the input; later levels include the earlier correct helpers.

The approval policy has three outcomes: a known refusal wins; otherwise an unknown answer leaves the request pending; otherwise it is accepted. The lab requires a code change and checks fresh input cases.

Start the lab

Report completion, preserve every supplied count even while inspection is incomplete, then combine optional approvals without losing their meaning.

  1. easy · F-CONDITIONS-V4-1 Report completion
  2. medium · F-CONDITIONS-V4-2 Report a supplied count
  3. hard · F-CONDITIONS-V4-3 Combine approvals
In the frontend A count of zero is still displayable data. Optional

A number supplied to a component may be zero. Test the representation of absence when deciding whether to show it. Conditional rendering uses the condition’s meaning; it does not decide your application’s presence policy.

Example.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
export function Count({ count }: { count: number | null }) {
  return count !== null ? <p>{count}</p> : <p>Pending</p>;
}

The conditional returns the count element even when the supplied number is zero.

Sources: React: Conditional rendering,Svelte: if blocks.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.