Recall & Review
beginner
What is a numeric enum in TypeScript?
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?<br>
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?
✗ 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?✗ 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?
✗ 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?
✗ Incorrect
After a custom value, following members increment from that value automatically.
Which of these is a valid numeric enum declaration?
✗ 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.