Why isn't the bug where it crashed?
Symptom and cause: how undefined and NaN quietly travel through a program, and why you search against the flow of data.
The line in the error is where JavaScript noticed the problem, not where it appeared. A bad value is born in one place — a function forgot to return, a search found nothing, an object lacks a field — and then quietly rides through the program: into a variable, into a parameter, into an object field. Only where someone tries to do the impossible with it does something go bang.
function getDiscount(user) {
if (user.isVip) {
return 0.2;
}
// forgot to return for regular customers
}
function finalPrice(price, user) {
const discount = getDiscount(user);
return price - price * discount;
}
function receipt(price, user) {
return "Total: $" + finalPrice(price, user);
}
console.log(receipt(1000, { isVip: true }));
console.log(receipt(1000, { isVip: false }));getDiscount, but it shows up on the receipt — two functions away. And not a single error in the console.Why didn't JavaScript stop us earlier? It was designed in 1995 to liven up web pages, and it was made forgiving: better to show something than break the page. So:
- a function without return silently returns undefined;
- reading a missing property gives undefined, not an error;
1000 * undefinedisNaN, andNaNin any arithmetic givesNaNagain;- find with no match returns undefined, and so does
arr[100]past the end of an array.
An error only happens when an operation is truly impossible: reading a property of undefined or null, calling a non-function. Until then, a bad value travels freely.
Hence the main technique: go against the flow of data. At the crash line ask: "where did this value come from?" — and step there. Then again: "and where did this come from?". Step by step, until you find the place where a good value turned bad. Like footprints in the snow — from the victim back to the culprit.
function getDiscount(user) {
if (user.isVip) {
return 0.2;
}
}
function finalPrice(price, user) {
const discount = getDiscount(user);
console.log("step 2, discount:", discount);
const result = price - price * discount;
console.log("step 1, result:", result);
return result;
}
finalPrice(1000, { isVip: false });result is NaN because discount is undefined. Next step: inside getDiscount.How do you make things crash closer to the cause? Check data where it enters the program: a server response, a form field, a line from a file. Convert types right away and throw a clear error — that's the idea from the errors chapter carried further: better to fail loudly and early than quietly and far away.
function toPrice(raw) {
const price = Number(raw);
if (Number.isNaN(price)) {
throw new TypeError("A price must be a number, got: " + raw);
}
return price;
}
console.log(toPrice("300"));
try {
toPrice("a hundred bucks");
} catch (error) {
console.log(error.message);
}Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.