decisions

const size = 10;
let bucket;

if (size > 100) bucket = "big";
else if (size > 4) bucket = "small";
else bucket = "tiny";

console.log(bucket);

Output:

small

Object lookup

const food = "salad";
const menu = { salad: "green", pizza: "round" };
console.log(menu[food] ?? "unknown");

Output:

green

A lookup table beats five else if branches when the mapping is flat The ?? keeps a legitimate empty string value from being replaced

switch

switch earns its place when one value drives many exact matches Remember break Forgetting it is the classic fall through bug

Ternary

const state = enabled ? "on" : "off";

Good for one choice Bad for a chain of four Readability wins

Next

Reusable code in 04 functions

Posted