Enums

Numeric enums

A numeric enum names a fixed set of numbers. Members get 0, 1, 2 by default, so they fit choices like directions or steps.

enum Direction { Up, Down, Left, Right, } const move: Direction = Direction.Up; console.log(move);

0

String enums

A string enum gives every member a readable text value. Logs and network payloads stay clear because the value explains itself.

enum Role { Admin = "ADMIN", Editor = "EDITOR", Guest = "GUEST", } const role: Role = Role.Admin; console.log(role);

ADMIN

const enums

A const enum is inlined at compile time and leaves no object behind at run time. Use it for hot paths where every byte and lookup counts.

const enum Size { Small, Medium, Large, } const box: Size = Size.Medium; console.log(box);

1

Prefer union types when possible

A union of literals often beats an enum: no extra syntax, exact autocompletion, and errors that name the allowed values.

Pick enums when values are shared across modules or need reverse lookup, and unions for small local choices.

type DirectionName = "up" | "down" | "left" | "right"; function walk(step: DirectionName): void { console.log(step); } walk("up");

up

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What value does the first member of a numeric enum get by default?

    Answer

    0

    Numeric members count from zero unless the first one gives a number of its own.

  2. What is the run-time value of a member of a string enum?

    Answer

    The text written after the equals sign

    A string enum stores the text itself, so logs and payloads show a readable value.

  3. What does a const enum leave in the compiled JavaScript?

    Answer

    Nothing: each use is replaced by its value

    The compiler inlines the member values and emits no object for the enum.

  4. When is a union of string literals a better fit than an enum?

    Answer

    When the choice is small and local to one file

    A small local choice needs no enum object, and the union names the allowed values in errors.

  5. What does this program print? enum Direction { Up, Down, Left, Right, } const move: Direction = Direction.Up; console.log(move);

    Answer

    0

    The first numeric member counts from zero, and the variable holds that number.

  6. What does this program print? enum Role { Admin = "ADMIN", Editor = "EDITOR", Guest = "GUEST", } const role: Role = Role.Editor; console.log(role);

    Answer

    EDITOR

    A string enum member is the text it was given, so the log shows EDITOR.

  7. What does this program print? const enum Size { Small, Medium, Large, } const box: Size = Size.Large; console.log(box);

    Answer

    2

    The members count from zero, so the third one is 2, and the compiler inlines it.

  8. What does this program print? type DirectionName = "up" | "down" | "left" | "right"; function walk(step: DirectionName): void { console.log(step); } walk("down");

    Answer

    down

    The call passes one of the allowed literals, and the function logs it.

  9. What does the compiler say about this program? enum Role { Admin = "ADMIN", Guest = "GUEST", } const role: Role = "ADMIN"; console.log(role);

    Answer

    error TS2322: Type '"ADMIN"' is not assignable to type 'Role'.

    A string enum accepts its members, not the raw text behind them, so the literal is rejected.

  10. What does the compiler say about this program? enum Role { Admin = "ADMIN", Guest = "GUEST", } console.log(Role.Moderator);

    Answer

    error TS2339: Property 'Moderator' does not exist on type 'typeof Role'.

    The enum has no such member, and the compiler names the enum object it looked on.

Article author: Arthur Isaev