Enum
Enumerated Type
- Pronunciation
- EE-num or EE-noom
In short
An enum is a data type that defines a fixed set of named values, such as an order's possible statuses, so code can't use a value outside that set.
What is an enum?
An enum, short for enumerated type, is a type whose possible values are a small, fixed list of named constants. Instead of storing an order's status as a loose string or number that could hold anything, you define OrderStatus with exactly Pending, Shipped and Delivered, and the compiler rejects anything else. Names like Color.Red also make code self-documenting compared with a bare 2.
Enums vary a lot between languages. In C, enum members are just named integers, so they can be mixed up with ordinary numbers. Java enums are full classes that can have fields and methods, while Rust and Swift let each variant carry its own data, turning enums into what functional languages call algebraic data types or tagged unions. TypeScript has an enum keyword, but many teams prefer a union of string literals such as "pending" | "shipped", which disappears after compilation.
An enum works like a multiple-choice question rather than a free-text box: the answer must be one of the listed options, so typos and invalid values are caught early. Enums pair naturally with switch or match statements, and in languages such as Rust and Swift the compiler refuses to build when a case isn't handled, so adding a new value reveals every place that needs updating.
Enums are sometimes confused with plain constants. A group of constants like const RED = 0 gives values names but no type safety, since any number can still be passed where a color is expected. An enum makes the set itself a type, so a function that takes a Color can only receive one of its members.
Key takeaways
- An enum is a type with a fixed set of named values.
- It prevents invalid values and reads better than magic numbers or strings.
- In C, enums are named integers; in Java, Rust and Swift they can carry methods or data.
- Exhaustive
matchorswitchchecks catch missing cases when a new value is added.
Example
enum OrderStatus {
Pending = "PENDING",
Shipped = "SHIPPED",
Delivered = "DELIVERED",
}
function label(status: OrderStatus): string {
switch (status) {
case OrderStatus.Pending: return "Waiting to ship";
case OrderStatus.Shipped: return "On the way";
case OrderStatus.Delivered: return "Arrived";
}
}
label(OrderStatus.Shipped); // "On the way", while label("LOST") won't compileReaders ask
When should I use an enum instead of strings?
Use an enum when a value must be one of a small, known set, such as statuses, roles or directions. It catches typos at compile time and lets editors autocomplete the options, while free-form strings accept anything.
Should I use enums in TypeScript?
Both TypeScript enums and unions of string literals work. Enums generate a real JavaScript object at runtime, while a union like "admin" | "user" exists only at compile time, which is why many codebases prefer unions for simple cases.
Can enums have methods?
In some languages, yes. Java, Kotlin, Swift and Rust let enums define methods, and Rust and Swift enums can also carry different data in each variant, while a C enum is only a set of named integer constants.
See also
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- TypeScriptWeb Development, p. 50TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
- JavaProgramming Languages, p. 15Java is a statically typed, object-oriented programming language that compiles to bytecode for the Java Virtual Machine, so programs run on many platforms.
- RustProgramming Languages, p. 29Rust is a compiled, statically typed systems language that provides memory safety without a garbage collector by checking ownership rules at compile time.
- SwiftProgramming Languages, p. 31Swift is a compiled, statically typed language designed for safety and speed, used mainly to build apps for iPhone, iPad, Mac, and other Apple platforms.
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
Spotted a mistake or something missing on this page?Suggest an edit