What is reverse mapping in numeric enums in TypeScript?
Reverse mapping means you can get the name of an enum member by using its numeric value. TypeScript creates this automatically for numeric enums.
Click to reveal answer
intermediate
How does TypeScript create reverse mapping for numeric enums?
TypeScript creates an object where both the name-to-value and value-to-name mappings exist. For example, Enum[Enum.Value] returns the name as a string.
Click to reveal answer
beginner
Can reverse mapping be used with string enums in TypeScript?
No, reverse mapping only works with numeric enums. String enums do not have reverse mapping because string values are not unique keys.
Click to reveal answer
beginner
Example: What will be the output of this code?
enum Color { Red, Green, Blue }
console.log(Color[1]);
The output will be "Green" because the numeric value 1 corresponds to the enum member Green.
Click to reveal answer
intermediate
Why is reverse mapping useful in TypeScript numeric enums?
It helps to get the name of an enum member from its numeric value, which is useful for debugging, logging, or displaying friendly names.
Click to reveal answer
What does reverse mapping in numeric enums allow you to do?
APrevent enums from being changed
BGet the enum member name from its numeric value
CAutomatically assign string values to enums
DConvert string enums to numeric enums
✗ Incorrect
Reverse mapping lets you use the numeric value to find the enum member's name.
Does reverse mapping work with string enums in TypeScript?
ANo, never
BOnly if values are unique
CYes, always
DOnly in TypeScript 4.0+
✗ Incorrect
Reverse mapping only works with numeric enums, not string enums.
Given enum Status { Active = 1, Inactive, Pending }, what is Status[2]?
A"Pending"
B"Active"
C"Inactive"
Dundefined
✗ Incorrect
Since Active=1, Inactive=2, Pending=3, Status[2] returns "Inactive".
What type of object does TypeScript create for numeric enums?
ABoth name-to-value and value-to-name mappings
BOnly value-to-name mapping
COnly name-to-value mapping
DNo mapping, just constants
✗ Incorrect
TypeScript creates an object with both mappings for numeric enums.
Why might reverse mapping be helpful when debugging?
AIt shows the numeric value instead of the name
BIt prevents errors in enum usage
CIt converts enums to strings automatically
DIt lets you see the enum member name from a numeric value
✗ Incorrect
Reverse mapping helps by showing the enum member name from its numeric value.
Explain how reverse mapping works in numeric enums in TypeScript and why it is useful.
Think about how you can find the name if you only have the number.
You got /3 concepts.
Describe the difference between numeric enums and string enums regarding reverse mapping.
Consider how keys and values are stored in the enum object.
You got /3 concepts.
Practice
(1/5)
1. What does reverse mapping in numeric enums in TypeScript allow you to do?
easy
A. Prevent enums from being changed at runtime
B. Convert enum names to strings automatically
C. Convert enum numeric values back to their corresponding names
D. Create string enums instead of numeric enums
Solution
Step 1: Understand numeric enums
Numeric enums assign numbers to names and create a two-way map.
Step 2: Understand reverse mapping
Reverse mapping lets you use the number to get the enum name back.
Final Answer:
Convert enum numeric values back to their corresponding names -> Option C
Quick Check:
Reverse mapping = number to name conversion [OK]
Hint: Remember: numeric enums map both ways: name↔number [OK]
Common Mistakes:
Thinking reverse mapping converts names to strings
Confusing string enums with numeric enums
Assuming enums are immutable at runtime
2. Which of the following is the correct syntax to access the name from a numeric enum value 2 in TypeScript?
easy
A. EnumName['name']
B. EnumName[2]
C. EnumName.getName(2)
D. EnumName.2
Solution
Step 1: Recall enum reverse mapping syntax
Numeric enums create a reverse map accessible by bracket notation with the number.
Step 2: Identify correct syntax
Using EnumName[2] returns the name for value 2.
Final Answer:
EnumName[2] -> Option B
Quick Check:
Bracket notation with number = Enum name [OK]
Hint: Use bracket notation with number to get enum name [OK]
Common Mistakes:
Using dot notation with a number (invalid syntax)
Trying to call a non-existent getName method
Using string keys instead of numeric keys
3. Given the enum:
enum Color { Red, Green, Blue }
What is the output of console.log(Color[1])?
medium
A. "Green"
B. "Red"
C. 1
D. undefined
Solution
Step 1: Understand enum default values
By default, Red=0, Green=1, Blue=2 in numeric enums.
Step 2: Use reverse mapping
Color[1] returns the name for value 1, which is "Green".
Final Answer:
"Green" -> Option A
Quick Check:
Color[1] = "Green" [OK]
Hint: Default enum starts at 0, so 1 maps to second name [OK]
Common Mistakes:
Assuming enum values start at 1
Expecting numeric output instead of string
Confusing enum name with value
4. Consider this enum:
enum Status { Active = 1, Inactive, Pending }
What is wrong with this code?
console.log(Status[0]);
medium
A. It returns undefined because 0 is not a valid enum value
B. It causes a syntax error
C. It returns "Active" incorrectly
D. It throws a runtime exception
Solution
Step 1: Check enum values
Status starts at 1, so valid values are 1, 2, 3 for Active, Inactive, Pending.
Step 2: Check reverse mapping for 0
Status[0] does not exist, so it returns undefined.
Final Answer:
It returns undefined because 0 is not a valid enum value -> Option A
Quick Check:
Invalid enum value returns undefined [OK]
Hint: Check enum start value before reverse mapping [OK]
Common Mistakes:
Expecting 0 to map to first enum name
Thinking it causes syntax or runtime error
Assuming reverse mapping always returns a string
5. Given this enum with custom values:
enum Direction { Up = 3, Down = 5, Left = 7, Right = 9 }
What will console.log(Direction[5]) output?
hard
A. 5
B. "Up"
C. undefined
D. "Down"
Solution
Step 1: Identify enum values
Direction enum assigns Up=3, Down=5, Left=7, Right=9 explicitly.
Step 2: Use reverse mapping
Direction[5] returns the name for value 5, which is "Down".
Final Answer:
"Down" -> Option D
Quick Check:
Reverse mapping works with custom numeric values [OK]
Hint: Reverse mapping works even with custom enum numbers [OK]
Common Mistakes:
Assuming reverse mapping only works with default values
Expecting numeric output instead of string
Confusing enum names with values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.