📘
Enums: Named Constants
Beginner
70 XP
25 min
Lesson Content
Enums in TypeScript
Enums allow you to define a set of named constants, making your code more readable and maintainable.
Numeric Enums
enum Direction {
Up,
Down,
Left,
Right
}
let move: Direction = Direction.Up;String Enums
enum Status {
Pending = "PENDING",
Approved = "APPROVED",
Rejected = "REJECTED"
}Using Enums
function getStatusMessage(status: Status): string {
switch (status) {
case Status.Pending:
return "Please wait...";
case Status.Approved:
return "Approved!";
case Status.Rejected:
return "Rejected.";
}
}Example Code
Create and use enums for constants
enum Priority {
Low = "LOW",
Medium = "MEDIUM",
High = "HIGH"
}
function getPriorityMessage(priority: Priority): string {
return `Priority: ${priority}`;
}
console.log(getPriorityMessage(Priority.High));Expected Output:
Priority: HIGH
Study Tips
- •Read the theory content thoroughly before practicing
- •Review the example code to understand key concepts
- •Proceed to the Practice tab when you're ready to code