📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Introduction
In this chapter, we will explore enums in TypeScript. Enums are a way to define a set of named constants. They can be used to represent a collection of related values, such as days of the week or directions. Understanding how to use enums is essential for improving code readability and maintainability in your TypeScript programs.
Table of Contents
- Definition
- Enum Syntax
- Numeric Enums
- String Enums
- Heterogeneous Enums
- Enum Members
- Using Enums
- Complete Example with Output
- Conclusion
Definition
An enum in TypeScript is a way to define a set of named constants. Enums can be numeric or string-based. They provide a way to define a collection of related values that can be used throughout your code to improve readability and maintainability.
Enum Syntax
Syntax
enum EnumName {
Constant1,
Constant2,
...
}
Example
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction);
Output
{ '0': 'Up', '1': 'Down', '2': 'Left', '3': 'Right', Up: 0, Down: 1, Left: 2, Right: 3 }
Numeric Enums
Numeric enums are the default in TypeScript. The first value has a numeric value of 0, and each subsequent value increases by 1.
Example
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction.Up); // Output: 0
console.log(Direction.Down); // Output: 1
console.log(Direction.Left); // Output: 2
console.log(Direction.Right); // Output: 3
Output
0
1
2
3
String Enums
String enums allow you to assign custom string values to each enum member.
Example
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
console.log(Direction.Up); // Output: UP
console.log(Direction.Down); // Output: DOWN
console.log(Direction.Left); // Output: LEFT
console.log(Direction.Right); // Output: RIGHT
Output
UP
DOWN
LEFT
RIGHT
Heterogeneous Enums
Heterogeneous enums allow you to mix string and numeric members within the same enum. However, it's not a common practice and should be used sparingly.
Example
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES"
}
console.log(BooleanLikeHeterogeneousEnum.No); // Output: 0
console.log(BooleanLikeHeterogeneousEnum.Yes); // Output: YES
Output
0
YES
Enum Members
Enum members can be constant or computed. Constant members are evaluated at compile-time, while computed members are evaluated at runtime.
Example
enum FileAccess {
None, // 0
Read = 1 << 1, // 2
Write = 1 << 2, // 4
ReadWrite = Read | Write, // 6
G = "123".length // 3
}
console.log(FileAccess.None); // Output: 0
console.log(FileAccess.Read); // Output: 2
console.log(FileAccess.Write); // Output: 4
console.log(FileAccess.ReadWrite); // Output: 6
console.log(FileAccess.G); // Output: 3
Output
0
2
4
6
3
Using Enums
Enums can be used in various ways, including as function parameters, return values, and in conditional statements.
Example
enum Direction {
Up,
Down,
Left,
Right
}
function move(direction: Direction) {
switch (direction) {
case Direction.Up:
console.log("Moving Up");
break;
case Direction.Down:
console.log("Moving Down");
break;
case Direction.Left:
console.log("Moving Left");
break;
case Direction.Right:
console.log("Moving Right");
break;
}
}
move(Direction.Up); // Output: Moving Up
move(Direction.Left); // Output: Moving Left
Output
Moving Up
Moving Left
Complete Example with Output
In this section, we will combine all the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.
TypeScript Code
You can test the following code in the TypeScript Playground:
// Numeric Enums
enum Direction {
Up,
Down,
Left,
Right
}
console.log(Direction.Up); // Output: 0
console.log(Direction.Down); // Output: 1
console.log(Direction.Left); // Output: 2
console.log(Direction.Right); // Output: 3
// String Enums
enum StringDirection {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT"
}
console.log(StringDirection.Up); // Output: UP
console.log(StringDirection.Down); // Output: DOWN
console.log(StringDirection.Left); // Output: LEFT
console.log(StringDirection.Right); // Output: RIGHT
// Heterogeneous Enums
enum BooleanLikeHeterogeneousEnum {
No = 0,
Yes = "YES"
}
console.log(BooleanLikeHeterogeneousEnum.No); // Output: 0
console.log(BooleanLikeHeterogeneousEnum.Yes); // Output: YES
// Enum Members
enum FileAccess {
None, // 0
Read = 1 << 1, // 2
Write = 1 << 2, // 4
ReadWrite = Read | Write, // 6
G = "123".length // 3
}
console.log(FileAccess.None); // Output: 0
console.log(FileAccess.Read); // Output: 2
console.log(FileAccess.Write); // Output: 4
console.log(FileAccess.ReadWrite); // Output: 6
console.log(FileAccess.G); // Output: 3
// Using Enums
function move(direction: Direction) {
switch (direction) {
case Direction.Up:
console.log("Moving Up");
break;
case Direction.Down:
console.log("Moving Down");
break;
case Direction.Left:
console.log("Moving Left");
break;
case Direction.Right:
console.log("Moving Right");
break;
}
}
move(Direction.Up); // Output: Moving Up
move(Direction.Left); // Output: Moving Left
Conclusion
In this chapter, we covered enums in TypeScript, including how to define numeric, string, and heterogeneous enums, use enum members, and using enums in functions and conditional statements. We provided a complete example with its output to illustrate how enums work in TypeScript.
Comments
Post a Comment
Leave Comment