0
0
Typescriptprogramming~5 mins

Enum member access in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an enum in TypeScript?
An enum is a way to define a set of named constants. It helps group related values under one name for easier use and better code clarity.
Click to reveal answer
beginner
How do you access a numeric enum member by its name?
You use dot notation with the enum name and member name, like EnumName.MemberName.
Click to reveal answer
intermediate
How can you get the name of an enum member from its numeric value?
TypeScript enums create a reverse mapping, so you can use EnumName[value] to get the member name as a string.
Click to reveal answer
beginner
What happens if you try to access an enum member that does not exist?
You get undefined because the member name or value is not found in the enum.
Click to reveal answer
intermediate
Can you access enum members using variables? How?
Yes, you can use bracket notation with a variable holding the member name or value, like EnumName[variable].
Click to reveal answer
How do you access the enum member 'Red' in enum Color { Red, Green, Blue }?
ARed.Color
BColor.Red
CColor->Red
DColor['Red']()
What does Color[0] return if enum Color { Red, Green, Blue }?
A'Green'
B0
C'Red'
Dundefined
If you try Color['Yellow'] and 'Yellow' is not a member, what is the result?
AThrows error
Bnull
CReturns 'Yellow'
Dundefined
Can you use a variable to access an enum member dynamically?
AYes, using bracket notation like Color[varName]
BNo, only dot notation works
CYes, but only with string enums
DNo, enums are static
What type of enum member access allows getting the member name from its value?
AReverse mapping in numeric enums
BDot notation
CBracket notation with string enums
DYou cannot get member name from value
Explain how to access enum members by name and by value in TypeScript.
Think about how you use the enum name and member name or value.
You got /3 concepts.
    Describe what happens when you try to access an enum member that does not exist.
    Consider what JavaScript returns when a property is missing.
    You got /3 concepts.