Good Code
The good version compares exact values and only falls back when retryCount is null or undefined, so 0 remains a valid explicit value.
Lesson 01
Compare values intentionally and preserve meaningful falsy values.
function formatReviewState(state) {
// Strict equality avoids coercion hiding unexpected values.
if (state === "approved") {
return "Approved";
}
if (state === "changes_requested") {
return "Changes requested";
}
return "Pending";
}
function getRetryCount(options) {
return options.retryCount ?? 3;
}function formatReviewState(state) {
// Loose equality and || blur missing data with valid falsy values.
if (state == "approved") {
return "Approved";
}
if (state == "changes_requested") {
return "Changes requested";
}
return "Pending";
}
function getRetryCount(options) {
return options.retryCount || 3;
}The good version compares exact values and only falls back when retryCount is null or undefined, so 0 remains a valid explicit value.
The bad version depends on coercion and uses ||, which treats valid falsy values like 0 as missing configuration.