0
0
Typescriptprogramming~5 mins

Numeric enums in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A0
B1
C-1
Dundefined
Given enum Status { Start = 5, Stop, Pause }, what is the value of Status.Pause?
A5
B6
C7
D8
How do you access the name of an enum member from its numeric value?
AEnumName[NumericValue]
BEnumName.NumericValue
CEnumName.getName(NumericValue)
DEnumName.valueOf(NumericValue)
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
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 }
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.