0Pricing
TypeScript Academy · Lesson

Enums: Numeric and String Constants

Use enums to replace magic strings and numbers.

Enums: Numeric and String Constants is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Enums let you define a set of named constant values. They replace magic numbers and strings with readable names that TypeScript enforces.

Numeric Enums

By default, enum members are assigned auto-incrementing numbers starting at 0.
enum Direction {
  Up,    // 0
  Down,  // 1
  Left,  // 2
  Right, // 3
}
console.log(Direction.Up); // 0

Custom Numeric Enum Values

You can set a starting value or override individual members. Subsequent members auto-increment from the last set value.
enum Status {
  Active = 1,
  Inactive, // 2
  Banned = 100,
}

String Enums

String enums require an explicit value for each member. They are more readable in logs and serialized data than numbers.
enum Color {
  Red = 'RED',
  Green = 'GREEN',
  Blue = 'BLUE',
}

Using Enums in Functions

Use an enum type as a parameter type to restrict values to the defined set.
function move(dir: Direction): void {
  console.log('Moving:', Direction[dir]);
}
move(Direction.Up);

Reverse Mapping (Numeric Only)

Numeric enums support reverse mapping: you can look up the member name from its value. String enums do not support this.
enum Status { Active = 1, Inactive = 2 }
console.log(Status[1]); // 'Active'
console.log(Status.Active); // 1

const Enums

`const enum` is inlined at compile time — no JavaScript object is emitted. This reduces bundle size but limits runtime introspection.
const enum Axis { X, Y, Z }
const a = Axis.X; // compiled to: const a = 0;

Enums vs Union of Literals

Many TypeScript developers prefer union literal types over enums. They are lighter-weight and work without runtime code.
// Enum
enum Dir { Up = 'UP', Down = 'DOWN' }

// Union literal (preferred by many)
type Dir2 = 'UP' | 'DOWN';

Heterogeneous Enums (Avoid)

Mixing string and numeric values in one enum is allowed but confusing. Avoid this pattern.
// Avoid:
enum Mixed { A = 1, B = 'bee' }

Enums Are Real Objects at Runtime

Unlike interfaces and type aliases, enums emit JavaScript code. The compiled output is a JavaScript object with both name-to-value and value-to-name mappings (for numeric enums).

Iterating Over Enum Values

Use Object.values() or Object.keys() on the enum object to iterate its members at runtime.
enum Role { Admin = 'ADMIN', User = 'USER', Guest = 'GUEST' }
const roles = Object.values(Role);
console.log(roles); // ['ADMIN', 'USER', 'GUEST']

Quick Check

Which enum type supports reverse mapping (looking up the name from the value)?

Recap

Enums define named constant sets as real JavaScript objects. Use string enums for readability, const enums for performance, and consider union literal types as a lighter alternative.

Frequently asked questions

Is the “Enums: Numeric and String Constants” lesson free?

Yes — the full text of “Enums: Numeric and String Constants” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Enums: Numeric and String Constants”?

Use enums to replace magic strings and numbers. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Enums: Numeric and String Constants” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Typed Arrays: T[] and Array
  2. Readonly Arrays and Immutable Data
  3. Tuples: Fixed-Length Typed Arrays
  4. Enums: Numeric and String Constants
← Back to TypeScript Academy