TypeScript Enums

What are Enums?

Enums define a set of named constant values.

They make code easier to read and manage.

Loading...
Output:

Numeric Enums

By default, enums use numbers starting from 0.

Loading...
Output:

String Enums

You can assign string values to enums.

Loading...
Output:

Using Enums

Loading...
Output:

Enums in Functions

Loading...
Output:

Enums in Objects

Loading...
Output:

Full Example

Loading...
Output:

Why Enums are Important

  • Represent fixed sets of values
  • Improve readability
  • Reduce errors
  • Used in real applications

Common Mistakes

  • Mixing numeric and string enums incorrectly
  • Not using enums when values are fixed
  • Confusing enums with union types

Practice

Create an enum for traffic lights and write a function that prints the meaning.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic