Enums
Enums group related named constants.
A constant is a fixed value with a clear name.
Like a labeled set of choices that belong together.
TypeScript has numeric enums and string enums.
Numeric enums
Members get numbers that go up by one by default.
You can set the starting value.
Numeric enums also map from the number back to the name.
Map means you can look up either side.
String enums
String enums assign readable string values.
Use them when values show up in logs, APIs, or JSON.
JSON is a common way to send data as text.
The words still make sense at runtime.
const enums
A const enum is inlined at compile time.
Inlined means the names turn into plain values in the output.
That can shrink the JavaScript a bit.
Some bundlers prefer plain unions instead.
A modern alternative
Many teams use a const object plus a union.
You get named values without enum quirks.
as const locks the object to exact literal values.
When to use an enum
Enums help when a fixed set of named choices is shared.
Prefer string enums when values leave your program.
Prefer a const object plus union when you want simpler JavaScript.
Enums name a fixed set of choices. String enums stay readable outside your code.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.