Skip to main content

Enum

Enumerated Type

Pronunciation
EE-num or EE-noom
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/enum

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 match or switch checks catch missing cases when a new value is added.

Example

A string enum in TypeScripttypescript
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 compile

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings