Enum Data Type Example: Meaning, Syntax, and Uses

How-to basics · Updated November 22, 2026

An enum data type is a type whose values come from a fixed set of named choices. For example, a TrafficLight enum might allow only Red, Yellow, or Green; the exact syntax depends on the programming language.

A Simple Enum Data Type Example

Suppose an order can be in one of four states: pending, paid, shipped, or cancelled. Without an enum, a program might store the state as an ordinary string. That leaves room for accidental variations such as "shiped", "Paid", or "in transit", even if those are not valid states in the application.

An enum gives the choices a shared name and makes the intended set explicit. In pseudocode, the definition could look like this:

enum OrderStatus {
    Pending,
    Paid,
    Shipped,
    Cancelled
}

A variable of type OrderStatus should hold one of those named options, such as OrderStatus.Paid. The enum is not a list of every order; it defines the category of values that an individual order’s status may have. This distinction is useful whenever a field represents one choice from a known set.

What an Enum Is and How It Works

The word enum is short for enumeration: a naming of possible values. A programmer declares the set, and code can then refer to its members by name instead of repeating unexplained numbers or strings. Depending on the language, an enum may be checked by the compiler, represented internally by an integer, represented by a string, or implemented as objects with associated data.

Enums are often used in conditions and branching logic. For example, an application might show a tracking message based on an order’s status. A switch statement or equivalent can handle each defined member, making the possible cases visible to a reader. Some languages or tools can also warn when a branch does not account for all members, although the exact checks depend on the language and compiler settings.

Enums do not automatically enforce every business rule. Declaring a status enum does not guarantee that an order can move directly from Pending to Shipped. Rules about valid transitions still need to be implemented separately. The enum controls which values are representable; application logic determines which changes are allowed and when.

Enum Syntax in Common Programming Languages

Enum syntax and behavior vary, so an example should always be read in its language’s context. These short examples all express a small set of named choices, but they do not have identical runtime or storage behavior.

  • Python: The standard library provides Enum. A definition can be written as from enum import Enum, followed by class OrderStatus(Enum): and indented members such as PENDING = 1 and PAID = 2. Code refers to a member as OrderStatus.PAID. Python enums are classes; their values and comparison behavior depend on the enum type used.
  • Java: A typical declaration is enum OrderStatus { PENDING, PAID, SHIPPED, CANCELLED }. A variable can use the type OrderStatus, and code can refer to OrderStatus.PAID. Java enum constants are objects, not simply interchangeable integer codes. Java enums can also have fields, constructors, and methods.
  • C: A declaration such as enum OrderStatus { PENDING, PAID, SHIPPED, CANCELLED }; defines named enumerators. In C, enumerators have integer values, and the enum’s compatible integer type is determined by the implementation under the language rules. C enums therefore do not provide the same strict separation as Java enum objects.
  • TypeScript: TypeScript supports declarations such as enum OrderStatus { Pending, Paid, Shipped, Cancelled }. By default, these members receive numeric values in sequence. TypeScript also supports string enums, for example enum OrderStatus { Pending = "pending", Paid = "paid" }. TypeScript’s type system and generated JavaScript behavior should both be considered when choosing this feature.

These examples illustrate why there is no single universal enum syntax. Before copying an example, check whether the language expects a declaration keyword, explicit values, a particular member naming style, or an import. Also check how that language converts the enum for JSON, database storage, or another system: a member’s source-code name is not necessarily its serialized representation.

Named Values, Numeric Codes, and Stored Data

Some enums have implicit numeric values; others use explicit numbers or strings. A numeric representation can be compact and convenient inside a program, but an unexplained value such as 2 is difficult to interpret outside the declaration. A string such as "shipped" is more readable in logs and data files, but it takes more space and can still be mistyped if it is handled as an ordinary string.

Do not assume that an enum’s displayed name, internal value, and stored value are the same thing. In one language, Paid might be a named member whose underlying value is an integer. In another design, it may map to the string "paid". If a database or API depends on a particular representation, define and document that mapping rather than relying on an implicit default.

Stable representations matter when data outlives a program version. If numeric enum values are stored in files or databases, changing their assigned numbers can make old records mean something different. If strings are stored, renaming a member can break consumers that expect the previous spelling. For persistent data or public interfaces, use deliberate values and plan how older data will be read during changes.

When an Enum Is a Good Choice

An enum is a strong fit when a value has a relatively small, known set of alternatives and those alternatives are meaningful to the program. It can improve readability, make invalid options harder to express, and help tools identify places that need updating when the set changes.

Common uses include:

  • Workflow states such as Draft, Published, and Archived.
  • Direction choices such as North, South, East, and West.
  • Modes or categories such as Light and Dark themes, or a limited set of access levels.
  • Results from a function, such as Success, NotFound, and PermissionDenied, when the language or project design uses an enum for such outcomes.

Before choosing one, ask whether the set is genuinely controlled and whether each value is one option rather than a combination of independent options. If users or administrators can create new categories while the program runs, a database table or configurable collection may be more suitable than a compiled enum. If two independent yes-or-no properties can both be true, a single enum with four combined states might be less clear than two separate fields.

Common Enum Problems and How to Avoid Them

A frequent error is treating an enum like a free-form string. A value typed from user input, a URL, or a file may not match any declared member. Parse and validate external input at the boundary of the application, and decide what should happen for unknown values. Depending on the situation, the program might reject the input, report a clear error, or map it to an explicit fallback. Silently accepting an arbitrary value can undermine the benefit of the enum.

Another problem is assuming every language handles unknown or newly added members the same way. An older client may receive a value introduced by a newer server. If the client cannot recognize it, a strict parser may fail; a permissive parser may preserve it as an unknown value or substitute a default. For APIs that evolve independently, document this behavior and test compatibility rather than assuming all clients update together.

Enums can also become awkward when they are overloaded with unrelated meanings. A large set of values that mixes payment status, delivery status, and customer decisions is harder to understand than separate enums for separate concepts. Likewise, adding a new member may require updating display text, validation, database constraints, and branching logic. Search for all uses and consider exhaustive checks or tests so a new option is not accidentally ignored.

Finally, avoid depending on the incidental order of enum members unless the language and design explicitly guarantee the behavior you need. Reordering declarations can alter implicit numeric values in some systems. If values are persisted or exchanged, assigning stable explicit representations is safer than assuming that the first, second, or third member will always have the same number.

Enums Compared with Constants and Other Alternatives

Named constants can replace magic numbers, but a group of constants does not always create a distinct type. For example, constants named STATUS_PAID and STATUS_SHIPPED may make code easier to read, yet a language may still allow an unrelated integer to be passed where a status is expected. An enum can provide a stronger grouping and, in languages with strict enum typing, reduce that kind of mix-up.

A string or number is reasonable when the value is open-ended, supplied by users, or defined by an external standard that changes independently. A set of booleans can suit independent facts, such as whether a feature is enabled and whether it is visible. It is less suitable for alternatives that must be mutually exclusive: several booleans can accidentally represent contradictory combinations.

Some languages offer algebraic data types, tagged unions, or sealed class hierarchies for choices that carry different data. For example, a result might be either a successful response containing a value or a failure containing an error description. A basic enum can name Success and Failure, but it cannot by itself carry those different payloads in every language. Use the richer construct when each alternative needs its own associated information.

Frequently Asked Questions

What is a simple enum example?
A traffic-light enum with the members Red, Yellow, and Green is a simple example. A variable of that enum type represents one of those choices, not an arbitrary color name.

Is an enum a string or a number?
Neither universally. An enum is a language-level type or construct, and its members may be represented as numbers, strings, objects, or another form depending on the language and declaration. Check the rules for the language and the format used when data is stored or transmitted.

Why use an enum instead of a string?
An enum makes the allowed choices explicit and can let the compiler or development tools catch misspellings and invalid assignments. A string is more flexible, but that flexibility means the program usually needs its own validation.

Can an enum have a default value?
That depends on the language and how the variable is initialized. Some languages give particular variables or enum declarations default behavior; others require initialization or permit an invalid or unset state. Choose a default deliberately, and do not assume the first listed member is a safe business default.

Can you add new enum values later?
Usually you can change the source definition, but every consumer may need to handle the added choice. If values are stored or sent through an API, plan for older software encountering the new member and preserve any representations that existing data relies on.

Are enums suitable for database columns?
They can be, but the right design depends on the database, how often choices change, and whether other applications share the data. A database enum type, a constrained text column, or a separate lookup table each has different migration and portability trade-offs, so define the storage contract intentionally.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.