Conditionals and Logic
Guard Returns
Exit a function early when a condition fails.
Guard Returns
guard_return.js
const quantity = 2;
function describeOrder(count) {
if (count <= 0) {
return "empty";
}
return "items=" + count;
}
const summary = describeOrder(quantity);
console.log("quantity=" + quantity);
console.log("summary=" + summary);
guard-return
A guard return handles an exceptional or empty case first. The rest of the function can then read like the normal path.