Lesson 14 / 26

Numeric Enums

A named set of numeric constants.

A numeric enum

enum groups related constants under one name, giving each member a numeric value.

enum Status {
  Pending,   // 0
  Active,    // 1
  Closed,    // 2
}

let s: Status = Status.Active;
console.log(s);          // 1
console.log(Status[1]);  // "Active"

Auto-increment & custom start

Values default to 0, 1, 2, ... in order. Set the first explicitly — Pending = 1 — and the rest continue from there.

Reverse mapping

Numeric enums support a reverse lookup — Status[1] gives "Active" — string enums don't have this.

Quick check: In `enum Status { Pending, Active, Closed }`, what is `Status.Closed`?

  • 0
  • 1
  • 2
Answer

2 — Members default to 0, 1, 2 in declaration order, so Closed is 2.