Summary - Enum
- TypeScript course
- Beginner
- Lesson 24/89
- 4 min
Write an order status tracker using enums.
Enum summary
An enum groups named constants under one name. A numeric enum auto-increments and supports reverse mapping. A string enum gives readable string values. A const enum inlines values at use sites at compile time. For structured data such as order status tracking, an enum provides both type safety and readability.
enum SiparisDurumu { Bekle = "bekle", Teslim = "teslim" }
let durum: SiparisDurumu = SiparisDurumu.Bekle;
console.log(durum); // "bekle"Choose numeric, string or const enum according to the need.
Practice Order status tracking program
Define a const named SiparisDurumu mapping Bekle to "bekle" and Teslim to "teslim" (string enum behavior). Declare a variable named durum and set its value to SiparisDurumu.Teslim. Print the value of durum with console.log.
Starter code
// SiparisDurumu adında bir const tanımla
// Bekle "bekle" ve Teslim "teslim" ile eşle
// durum değişkenini SiparisDurumu.Teslim ile tanımla
// durum değerini yazdırSample solution
const SiparisDurumu = { Bekle: "bekle", Teslim: "teslim" };
let durum = SiparisDurumu.Teslim;
console.log(durum);Expected output
teslim
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which enum is suitable for showing readable values in logs?
- const enum Durum { Bekle, Hazir }
- enum Durum { Bekle = 0, Hazir = 1 }
- enum Durum { Bekle = "bekle", Hazir = "hazir" } Correct answer
- enum Durum { Bekle, Hazir }
Explanation: A string enum shows meaningful text instead of numbers in logs; the numeric and const enum options produce only numbers.
2 Complete the order status enum
enum SiparisDurumu { Bekle = "bekle", Gonderildi = ____ }
enum SiparisDurumu { Bekle = "bekle", Gonderildi = "gonderildi" }
Explanation: The <code>Gonderildi</code> member must have the value <code>"gonderildi"</code>; <code>"bekle"</code> belongs to another member, <code>"Gonderildi"</code> is wrong casing, and <code>"teslim"</code> is the wrong status.
3 Enum types summary
| Term | Meaning |
|---|---|
Numeric enum |
Auto-incrementing number constants |
String enum |
Readable string constants |
const enum |
Inlined at compile time |
Reverse mapping |
Value-to-name lookup |