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.
const checked = true;
const count = 0;
if (checked) console.log("complete");
if (count > 0) console.log("positive");
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.
const count: number | null = 0;
if (count) {
console.log("recorded");
} else {
console.log("missing");
}
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.
const approval: boolean | null = false;
console.log(approval !== null, Boolean(approval));
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.
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.
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
Sign in to keep your read marks on every device. Sign in
Was this helpful?