Data and validation bugs
Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.
What you will be able to do
#- Find a bug caused by trusting the shape of external data.
- Validate at the boundary and fail at the edge instead of deep in the code.
Overview
#Data bugs come from trusting the shape of a value that crossed a boundary. The value came from user input, a JSON response, a query string, local storage or another service, so its type is a promise you did not keep.
The first trap is coercion. A value that looks like a number can be a string, and string plus number concatenates instead of adding.
function total(price, tax) {
return price + tax;
}
If price arrives as the string 25 from an input, total returns 2510 rather than 35. The fix is to parse and validate at the boundary, then work with the parsed value.
function total(price, tax) {
const p = Number(price);
const t = Number(tax);
if (!Number.isFinite(p) || !Number.isFinite(t)) {
throw new TypeError('price and tax must be numeric');
}
return p + t;
}
The second trap is the difference between null, undefined and an empty value. A missing field and a field that is explicitly null often mean different things, and a falsy check conflates them with zero and the empty string.
// wrong: treats 0 and the empty string as missing
const label = input.label || 'Untitled';
// right: only null and undefined fall back
const label = input.label ?? 'Untitled';
The third trap is drift between two sides of a system. The client assumes a field is always present, the server starts omitting it, and the mismatch only appears for some records. Parse the response into a typed shape at the boundary so a violation fails there, with the field name, instead of three functions later.
Normalise before you compare. Trim whitespace, decide case sensitivity, and handle unicode so that two values that look the same compare the same. Validation belongs at the edge of the system, where the untrusted value enters; deeper code should receive values it can trust.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Debug
This function adds a price and a tax that arrive as strings from a form, so it concatenates instead of adding. Fix it to parse and validate both values at the boundary and reject non-numeric input.
starter.javascriptfunction total(price, tax) { return price + tax; }Reveal the reference answer
reference.javascriptfunction total(price, tax) { const p = Number(price); const t = Number(tax); if (!Number.isFinite(p) || !Number.isFinite(t)) { throw new TypeError('price and tax must be numeric'); } return p + t; }Authored expected output: total('25', '10') returns 35; total('25', 'abc') throws a TypeError instead of returning '25abc'.
The original uses the plus operator on strings, which concatenates. Parsing to numbers and checking finiteness turns a silent wrong value into an explicit error at the boundary where the untrusted input arrives.
Exercise 2Predict
A field is optional and may be null, 0 or an empty string. Which expression falls back to a default only when the field is null or undefined?
Common misconceptions
#- A value from an input element is already the right type.
- A falsy check is a safe way to detect a missing value.
Follow-up practice
#- debugging-review-17-accessibility-bug-hunts
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.