Numeric enums let you group related numbers with names. This makes your code easier to read and understand.
Numeric enums in Typescript
enum Name {
Member1 = 1,
Member2,
Member3 = 10,
Member4
}If you don't assign a number to the first member, it starts at 0 by default.
Members without assigned numbers get the previous member's number plus one.
enum Direction {
Up = 1,
Down,
Left,
Right
}enum Status {
Ready,
Waiting,
Done
}enum Color {
Red = 5,
Green = 10,
Blue
}This program defines days Monday to Friday starting at 1. It checks if a day is weekend (6 or 7). Monday prints as 1, Wednesday as 3, and Monday is not weekend.
enum Weekday {
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday
}
function isWeekend(day: Weekday): boolean {
return day === 6 || day === 7;
}
console.log(Weekday.Monday); // 1
console.log(Weekday.Wednesday); // 3
console.log(isWeekend(Weekday.Monday)); // falseNumeric enums can be used in comparisons and switch statements easily.
You can get the name from the number using reverse lookup, like Weekday[1] gives 'Monday'.
Be careful to assign numbers if you want specific values; otherwise, they auto-increment.
Numeric enums give names to numbers for clearer code.
Members auto-increment if no number is assigned.
Use enums to avoid magic numbers and improve readability.
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
