A numeric enum is a way to define a set of named constants with numeric values. By default, the first member is 0, and the rest increment by 1 automatically.
Click to reveal answer
beginner
How does TypeScript assign values to numeric enum members if no values are specified?
TypeScript starts the first member at 0 and assigns each following member a value incremented by 1 from the previous member.
Click to reveal answer
intermediate
Can you assign custom numeric values to enum members in TypeScript?
Yes, you can assign specific numeric values to any enum member. Subsequent members without values will increment from the last specified value.
Click to reveal answer
beginner
What is the output of this code?
enum Colors { Red, Green, Blue }
console.log(Colors.Green);
The output is 1 because Red is 0 by default, and Green is the next member, so it gets 1.
Click to reveal answer
intermediate
How can you get the name of an enum member from its numeric value in TypeScript?
TypeScript enums have reverse mapping. You can use the numeric value as a key to get the member name, for example: Colors[1] returns 'Green'.
Click to reveal answer
What is the default value of the first member in a numeric enum if not specified?
A0
B1
C-1
Dundefined
✗ Incorrect
By default, the first member of a numeric enum is assigned the value 0.
Given enum Status { Start = 5, Stop, Pause }, what is the value of Status.Pause?
A5
B6
C7
D8
✗ Incorrect
Since Start is 5, Stop is 6, and Pause is 7 by automatic increment.
How do you access the name of an enum member from its numeric value?
AEnumName[NumericValue]
BEnumName.NumericValue
CEnumName.getName(NumericValue)
DEnumName.valueOf(NumericValue)
✗ Incorrect
TypeScript enums support reverse mapping, so you can use EnumName[NumericValue] to get the member name.
What happens if you assign a custom value to a middle enum member?
AAll members reset to 0
BSubsequent members increment from that custom value
CIt causes a syntax error
DSubsequent members keep default values starting at 0
✗ Incorrect
After a custom value, following members increment from that value automatically.
Which of these is a valid numeric enum declaration?
Aenum Days { Mon = true, Tue, Wed }
Benum Days { Mon = 'Monday', Tue, Wed }
Cenum Days { Mon = null, Tue, Wed }
Denum Days { Mon = 1, Tue, Wed }
✗ Incorrect
Numeric enums must have numeric values or auto-incremented numbers. Strings or booleans are not valid for numeric enums.
Explain how numeric enums work in TypeScript and how values are assigned by default.
Think about how counting numbers start and how enums assign numbers automatically.
You got /4 concepts.
Describe how you can retrieve the name of an enum member if you only have its numeric value.
Remember enums in TypeScript can be used both ways: name to number and number to name.
You got /3 concepts.
Practice
(1/5)
1. What is a numeric enum in TypeScript used for?
easy
A. To declare variables without types
B. To assign names to numeric values for better code clarity
C. To define functions with numeric parameters
D. To create string-only constants
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 B
Quick Check:
Numeric enums = named numbers [OK]
Hint: Enums name numbers to avoid magic numbers [OK]
Common Mistakes:
Confusing enums with string constants
Thinking enums declare variables
Assuming enums define functions
2. Which of the following is the correct syntax to declare a numeric enum in TypeScript?
easy
A. enum Colors { Red = 1, Green, Blue }
B. enum Colors = { Red: 1, Green: 2, Blue: 3 }
C. enum Colors (Red = 1, Green, Blue)
D. enum Colors { Red: 1; Green; Blue }
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 A
Quick Check:
Correct enum syntax uses braces and '=' [OK]
Hint: Use braces and '=' for enum members [OK]
Common Mistakes:
Using '=' after enum name
Using colons instead of '=' for values
Separating members with semicolons
3. What will be the output of this code?
enum Status {
Ready = 3,
Waiting,
Done
}
console.log(Status.Waiting);
medium
A. 3
B. 5
C. 4
D. undefined
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 C
Quick Check:
Auto-increment after 3 = 4 [OK]
Hint: Next enum value auto-increments by 1 [OK]
Common Mistakes:
Assuming Waiting equals 3
Thinking enum values reset automatically
Expecting string output
4. Identify the error in this enum declaration:
enum Direction {
Up = 1,
Down = 2
Left = 3,
Right = 4
}
medium
A. Cannot mix assigned and unassigned members
B. Enum members cannot have assigned values
C. Enum name must be lowercase
D. Missing comma after Down = 2
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 D
Quick Check:
Enum members need commas [OK]
Hint: Check commas between enum members [OK]
Common Mistakes:
Forgetting commas between members
Thinking enum names must be lowercase
Believing all members must be unassigned
5. Given this enum:
enum Levels {
Low = 1,
Medium = Low * 2,
High = Medium + 2
}