Menu

Enum

06 / 10 Part of TypeScript Basics

What is an Enum?


An enum (short for "enumeration") lets you define a set of named constants — useful when a value can only be one of a small, fixed list of options, like days of the week or order statuses.



Numeric Enums


By default, enum members are assigned numbers automatically, starting at 0.


enum Direction {
    Up,
    Down,
    Left,
    Right
}

let move: Direction = Direction.Up;
console.log(move); // 0


Custom Starting Values


enum Direction {
    Up = 1,
    Down,
    Left,
    Right
}

console.log(Direction.Up);    // 1
console.log(Direction.Down);  // 2
console.log(Direction.Right); // 4

Once you set a starting value, each following member increments automatically from there.



String Enums


String enums require every member to have an explicit string value, and tend to be more readable when debugging or logging.


enum Status {
    Active = "ACTIVE",
    Inactive = "INACTIVE",
    Pending = "PENDING"
}

let currentStatus: Status = Status.Active;
console.log(currentStatus); // "ACTIVE"


Using Enums in Practice


enum OrderStatus {
    Placed = "PLACED",
    Shipped = "SHIPPED",
    Delivered = "DELIVERED",
    Cancelled = "CANCELLED"
}

function printStatus(status: OrderStatus): void {
    switch (status) {
        case OrderStatus.Placed:
            console.log("Your order has been placed.");
            break;
        case OrderStatus.Shipped:
            console.log("Your order is on its way.");
            break;
        case OrderStatus.Delivered:
            console.log("Your order has arrived.");
            break;
        case OrderStatus.Cancelled:
            console.log("Your order was cancelled.");
            break;
    }
}

printStatus(OrderStatus.Shipped); // Your order is on its way.


Reverse Mapping (Numeric Enums Only)


Numeric enums let you look up the name from the value — something string enums cannot do.


enum Direction {
    Up,
    Down
}

console.log(Direction[0]); // "Up"
console.log(Direction[1]); // "Down"


const Enums


Adding const before an enum tells the compiler to fully remove it during compilation and inline its values directly — resulting in smaller, faster generated JavaScript.


const enum Direction {
    Up,
    Down
}

let move = Direction.Up; // compiles down to: let move = 0;


Enums vs Union Literal Types


A common modern alternative to enums is a union of string literals, which avoids generating any extra JavaScript at all.


type Status = "ACTIVE" | "INACTIVE" | "PENDING";

let currentStatus: Status = "ACTIVE";
currentStatus = "DONE"; // Error: not a valid Status value



   
   
       
       
       
   
EnumsUnion Literal Types
Generate actual JavaScript code at compile timeExist only during compilation — zero runtime cost
Support reverse mapping (numeric enums)Cannot look up a "name" from a value
Good when you need an actual runtime objectGood for simple, lightweight type constraints



    Coming up next: Classes and object-oriented programming — bringing everything together with structured, reusable blueprints.