0
0
Typescriptprogramming~5 mins

Why enums are needed in Typescript - Quick Recap

Choose your learning style9 modes available
Recall & Review
beginner
What is an enum in TypeScript?
An enum is a special TypeScript feature that lets you define a set of named constants. It helps group related values under a single name.
Click to reveal answer
beginner
Why do we use enums instead of plain strings or numbers?
Enums make code easier to read and maintain by giving meaningful names to values. They also help catch errors early by restricting values to a fixed set.
Click to reveal answer
intermediate
How do enums improve code safety?
Enums restrict variables to only the predefined set of values, preventing invalid values and reducing bugs caused by typos or wrong inputs.
Click to reveal answer
beginner
Give a real-life example where enums are useful.
For example, days of the week can be an enum: Monday, Tuesday, etc. This avoids mistakes like misspelling 'Monday' or using invalid days.
Click to reveal answer
intermediate
What happens if you use plain strings instead of enums for fixed sets of values?
Using plain strings can lead to mistakes like typos, inconsistent values, and harder-to-maintain code because there is no central list of allowed values.
Click to reveal answer
What is the main benefit of using enums in TypeScript?
AThey replace functions.
BThey make the program run faster.
CThey group related constants with meaningful names.
DThey allow any value to be assigned.
Which problem do enums help prevent?
ATypos in fixed values.
BSlow internet connection.
CMemory leaks.
DInfinite loops.
How do enums improve code maintenance?
ABy removing all comments.
BBy centralizing allowed values in one place.
CBy making code shorter.
DBy hiding errors.
Which of these is a good use case for enums?
AStoring user passwords.
BHandling network requests.
CCalculating sums.
DRepresenting user roles like Admin, User, Guest.
What happens if you assign a value not in the enum to an enum variable?
ATypeScript will show an error.
BThe program crashes immediately.
CThe value is accepted silently.
DThe enum changes automatically.
Explain why enums are useful in TypeScript and how they help prevent errors.
Think about how enums limit what values a variable can have.
You got /5 concepts.
    Describe a real-life example where using an enum would make your code better.
    Consider something with a small list of known options.
    You got /4 concepts.