Design Patterns · Junior · Question 41 of 75
What's wrong with "magic numbers" like if (status === 3)?
- ✓Correct answer: The value's meaning is unclear, and it's easy to change it wrongly in one of many places
- 2The engine compares numbers in conditions more slowly than strings
- 3Numbers can't be used in
switch- only strings and enums
Explanation
A named constant ORDER_STATUS.SHIPPED explains the meaning and changes in one place. In TypeScript, a union of literals or an as const object is even better.