typescript
/

TypeScript Enums

Last Sync: Today

On this page

9
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Enums

What is an Enum?

Enums are a way to define a set of named constants in TypeScript, making code more readable and maintainable.

Numeric Enum

TypeScriptRead-only
1
enum Direction {
  Up,
  Down,
  Left,
  Right
}

Custom Values

TypeScriptRead-only
1
enum Status {
  Success = 200,
  NotFound = 404,
  Error = 500
}

String Enum

TypeScriptRead-only
1
enum Role {
  Admin = 'ADMIN',
  User = 'USER'
}

Using Enum

TypeScriptRead-only
1
let userRole: Role = Role.Admin;

Enum vs Object

FeatureEnumObject
Type SafetyYesNo
ReadabilityHighMedium
UsageConstantsGeneral data

Best Practices

  • Use enums for fixed values
  • Prefer string enums for readability
  • Use meaningful names
  • Avoid overusing enums

Common Mistakes

  • Using enums unnecessarily
  • Confusing numeric values
  • Not using descriptive names
  • Overcomplicating enums

Conclusion

Enums provide a structured way to define constants in TypeScript, improving readability and maintainability.

Try it yourself

enum Role { Admin, User }
let r = Role.Admin;

Test Your Knowledge

Q1
of 3

Enum used for?

A
Functions
B
Constants
C
Loops
D
API
Q2
of 3

String enum?

A
Yes
B
No
C
Only number
D
Only object
Q3
of 3

Enum improves?

A
Speed
B
Readability
C
Memory
D
API

Frequently Asked Questions

What is enum?

A set of named constants.

Types of enums?

Numeric and string enums.

Why use enums?

For readable constant values.

Previous

ts union intersection

Next

ts tuples

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.