What if you never had to remember or update magic numbers again?
Why Numeric enums in Typescript? - Purpose & Use Cases
Imagine you have a list of related values like days of the week or status codes, and you write each one as a separate constant number in your code.
For example, you write: const MONDAY = 0;, const TUESDAY = 1;, and so on.
This manual way is slow and error-prone because you have to remember each number and keep them consistent everywhere.
If you add or remove a day, you must update all the numbers manually, which can cause bugs and confusion.
Numeric enums let you group these related numbers under one name, automatically assigning numbers for you.
This keeps your code clean, easy to read, and safe from mistakes.
const MONDAY = 0; const TUESDAY = 1; const WEDNESDAY = 2;
enum Days {
Monday,
Tuesday,
Wednesday
}Numeric enums make your code easier to manage and understand by giving meaningful names to numbers automatically.
Think of a traffic light system where you assign numbers to colors: red = 0, yellow = 1, green = 2. Numeric enums let you write enum TrafficLight { Red, Yellow, Green } and use these names instead of numbers everywhere.
Manual number assignments are hard to track and update.
Numeric enums group related numbers with meaningful names.
They reduce errors and make code clearer and easier to maintain.
Practice
Solution
Step 1: Understand enum purpose
Numeric enums give meaningful names to numbers, making code easier to read.Step 2: Compare options
Only To assign names to numeric values for better code clarity correctly describes numeric enums as naming numbers for clarity.Final Answer:
To assign names to numeric values for better code clarity -> Option BQuick Check:
Numeric enums = named numbers [OK]
- Confusing enums with string constants
- Thinking enums declare variables
- Assuming enums define functions
Solution
Step 1: Recall enum syntax
TypeScript enums use curly braces with members separated by commas, and assignment uses '='.Step 2: Check each option
enum Colors { Red = 1, Green, Blue } uses correct syntax: enum name, braces, members with '=' and commas.Final Answer:
enum Colors { Red = 1, Green, Blue } -> Option AQuick Check:
Correct enum syntax uses braces and '=' [OK]
- Using '=' after enum name
- Using colons instead of '=' for values
- Separating members with semicolons
enum Status {
Ready = 3,
Waiting,
Done
}
console.log(Status.Waiting);Solution
Step 1: Understand enum auto-increment
Ready is set to 3, so Waiting becomes 4, and Done becomes 5 automatically.Step 2: Identify logged value
console.log(Status.Waiting) prints 4, the auto-incremented value after 3.Final Answer:
4 -> Option CQuick Check:
Auto-increment after 3 = 4 [OK]
- Assuming Waiting equals 3
- Thinking enum values reset automatically
- Expecting string output
enum Direction {
Up = 1,
Down = 2
Left = 3,
Right = 4
}Solution
Step 1: Check enum member separators
Enum members must be separated by commas. After 'Down = 2' there is no comma.Step 2: Validate other rules
Assigned values are allowed, enum names can be uppercase, and mixing assigned/unassigned is allowed.Final Answer:
Missing comma after Down = 2 -> Option DQuick Check:
Enum members need commas [OK]
- Forgetting commas between members
- Thinking enum names must be lowercase
- Believing all members must be unassigned
enum Levels {
Low = 1,
Medium = Low * 2,
High = Medium + 2
}What is the value of
Levels.High?Solution
Step 1: Calculate Medium value
Medium = Low * 2 = 1 * 2 = 2.Step 2: Calculate High value
High = Medium + 2 = 2 + 2 = 4.Step 3: Re-check calculations
Medium is 2, High is 4. This is correct.Step 4: Confirm enum constant expressions
TypeScript allows constant expressions. Low=1, Medium=2, High=4.Final Answer:
4 -> Option AQuick Check:
Levels.High = 4 [OK]
- Ignoring expression evaluation order
- Assuming High equals Medium + Low
- Confusing enum values with indexes
Ready to prove it?
Pass the test and this pattern moves unknown
