Enums
Learn how to use enumerations for better code clarity.
Enums is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Enums
Welcome to the next lesson! In this lesson, you’ll learn about enums in TypeScript. Enums allow you to define a set of named constants, making your code more readable and easier to manage. Let’s dive in!

2
What Are Enums?
An enum (short for enumeration) is a data type that allows you to define a set of named constants. Enums are used to group related values together, making your code more organized and less error-prone.
Example: Days of the week can be represented using an enum:
Task: Create an enum for months of the year and log some values.
enum Days {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
console.log(Days.Monday); // Output: 0
console.log(Days.Friday); // Output: 4
3
Numeric Enums
Enums in TypeScript are numeric by default, starting with 0. You can assign custom values to enum members if needed:
Task: Define an enum for user roles (e.g., Admin, Editor, Viewer) with custom numeric values.
enum Status {
Active = 1,
Inactive = 2,
Pending = 3,
}
console.log(Status.Active); // Output: 1
console.log(Status.Pending); // Output: 3
4
Reverse Mapping
TypeScript enums support reverse mapping, allowing you to get the name of an enum member from its value:
Task: Experiment with reverse mapping using your own enum.
enum Colors {
Red = 1,
Green,
Blue,
}
console.log(Colors[1]); // Output: "Red"
console.log(Colors[2]); // Output: "Green"
5
String Enums
TypeScript also supports string enums, where each enum member is assigned a string value:
Task: Create a string enum for primary colors (e.g., Red, Green, Blue).
enum Directions {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
console.log(Directions.Up); // Output: "UP"
console.log(Directions.Left); // Output: "LEFT"
6
Heterogeneous Enums
An enum can contain both numeric and string values, but this is generally discouraged for consistency:
Task: Define a mixed enum and observe its behavior in your editor.
enum Mixed {
Yes = 1,
No = "NO",
}
console.log(Mixed.Yes); // Output: 1
console.log(Mixed.No); // Output: "NO"
7
Using Enums in Functions
Enums are great for controlling the flow of your code. Example:
Task: Write a function that takes an enum as a parameter and returns a message.
enum Status {
Active,
Inactive,
Pending,
}
function checkStatus(status: Status): string {
switch (status) {
case Status.Active:
return "The status is active.";
case Status.Inactive:
return "The status is inactive.";
case Status.Pending:
return "The status is pending.";
default:
return "Unknown status.";
}
}
console.log(checkStatus(Status.Active)); // Output: "The status is active."
8
Common Mistakes
Here are some common mistakes when using enums:
- Using numeric enums when string enums would make the code clearer.
- Mixing numeric and string values in the same enum.
- Not using enums for values that could benefit from named constants.
Tip: Choose the appropriate enum type (numeric or string) based on your use case.
9
enum Fruits {
Apple = 1,
Banana,
Cherry,
}
console.log(Fruits.Banana);
10
Great Job!
Congratulations! You’ve learned how to use enums in TypeScript to define named constants. Enums are a powerful way to make your code more readable and less error-prone. In the next lesson, we’ll explore literal types, another advanced feature of TypeScript. Let’s keep coding!

Frequently asked questions
Is the “Enums” lesson free?
Yes — the full text of “Enums” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Enums”?
Learn how to use enumerations for better code clarity. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enums” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.