Back to Interview PrepGet interview ready with AILearn each topic the way interviewers ask it, then answer like you would in the interview.
Course progress0/12 chapters · 0%

Chapter 6 of 13

~12 min

Enums

An enum gives a readable name to a fixed set of related constant values, such as the days of the week or the possible states of an order, instead of scattering raw numbers or strings throughout the code.

Numeric Enums

By default, an enum is numeric: the first member is assigned `0`, and each subsequent member increments by one unless given its own explicit value. This is convenient, but the numbers are somewhat arbitrary and can shift unexpectedly if members are reordered or inserted, which is one reason numeric enums are used more cautiously in larger codebases.

Try it yourself

enum Direction {
  Up,    // 0
  Down,  // 1
  Left,  // 2
  Right, // 3
}

function move(direction: Direction): void {
  console.log('Moving:', Direction[direction]);
}

move(Direction.Left); // 'Moving: Left'
console.log(Direction.Up); // 0
Console

No output yet. Use console.log() to print values here.

String Enums

A string enum requires every member to have its own explicit string value, and does not auto-increment. Unlike numeric enums, string enums do not support reverse lookup (going from the value back to the member name), but their values are far more meaningful in logs, debugging output, and serialized data, since you see `"PENDING"` instead of an opaque `0`.

Try it yourself

enum OrderStatus {
  Pending = 'PENDING',
  Shipped = 'SHIPPED',
  Delivered = 'DELIVERED',
  Cancelled = 'CANCELLED',
}

function logStatus(status: OrderStatus): void {
  console.log('Order status:', status);
}

logStatus(OrderStatus.Shipped); // 'Order status: SHIPPED'
Console

No output yet. Use console.log() to print values here.

const enum

Prefixing an enum with `const` (`const enum Direction { ... }`) tells the compiler to inline every reference to a member at each usage site during compilation, rather than generating a lookup object at runtime. This produces smaller, faster output, but it also means the enum object itself does not exist at runtime, so it cannot be iterated over or used in reverse-lookup scenarios - a plain trade-off between runtime footprint and flexibility.

Preferring a Union of String Literals

Many TypeScript style guides now prefer a union of string literal types, like `type OrderStatus = 'PENDING' | 'SHIPPED' | 'DELIVERED' | 'CANCELLED'`, over an actual `enum`. A literal union requires no import at runtime (it disappears entirely, unlike a standard enum, which does generate a real JavaScript object), works naturally with plain string values coming from JSON or an API, and integrates more smoothly with generic utility types and template literal types elsewhere in the type system.

Try it yourself

type OrderStatus = 'PENDING' | 'SHIPPED' | 'DELIVERED' | 'CANCELLED';

function logStatus(status: OrderStatus): void {
  console.log('Order status:', status);
}

logStatus('SHIPPED'); // OK - a plain string literal satisfies the union
// logStatus('IN_TRANSIT'); // Error: not assignable to OrderStatus
Console

No output yet. Use console.log() to print values here.

Coding challenge

Define a string enum TrafficLight with members Red = "RED", Yellow = "YELLOW", Green = "GREEN". Write a function nextLight that takes a TrafficLight and returns the light that should come next in the standard Red -> Green -> Yellow -> Red cycle.

// Write your solution here

Console

No output yet. Use console.log() to print values here.

Answer every question below to mark this chapter as complete.

Quiz Yourself

Score: 0/5

1. By default, what value does the first member of a numeric enum receive, and how do subsequent members get their values?

2. What is a key difference between string enums and numeric enums?

3. What does prefixing an enum with `const` change about the compiled output?

4. Why might a team prefer `type OrderStatus = 'PENDING' | 'SHIPPED' | ...` over an actual `enum OrderStatus`?

5. What happens if you write `enum Status { Active = 1, Inactive }` (only the first member has an explicit value)?

You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in