JavaScript > TypeScript > TypeScript Basics > Enums
Simple TypeScript Enum Example
This code snippet demonstrates a basic TypeScript enum, showcasing its definition and usage for representing a set of named constants. Enums improve code readability and maintainability by providing a more descriptive way to represent values compared to using raw numbers or strings.
Defining a Simple Enum
This code defines an enum called `Color`. By default, enum members are assigned numerical values starting from 0. The `myColor` variable is declared with the type `Color` and assigned the value `Color.Green`. When printed to the console, it outputs the numerical value associated with `Color.Green`, which is 1.
enum Color {
Red, // 0
Green, // 1
Blue // 2
}
let myColor: Color = Color.Green;
console.log(myColor); // Output: 1
Concepts Behind Enums
Enums (enumerations) are a feature of TypeScript that allows you to define a set of named constants. They provide a way to organize and represent a collection of related values, making code more readable and maintainable. Instead of using magic numbers or hardcoded strings, you can use enums to represent specific states or options.
Real-Life Use Case
Consider a scenario where you're developing a game and need to represent different player states (e.g., Idle, Running, Jumping). Using an enum makes the code cleaner and more self-documenting compared to using arbitrary numbers or strings. Another common example is representing HTTP status codes.
Real-Life Use Case Code Example
This shows how an enum `PlayerState` can be used to manage different states in a game. The function `handlePlayerState` takes a `PlayerState` as input and performs actions based on the current state.
enum PlayerState {
Idle,
Running,
Jumping,
Falling
}
function handlePlayerState(state: PlayerState) {
switch (state) {
case PlayerState.Idle:
console.log("Player is idle.");
break;
case PlayerState.Running:
console.log("Player is running.");
break;
case PlayerState.Jumping:
console.log("Player is jumping.");
break;
case PlayerState.Falling:
console.log("Player is falling.");
break;
}
}
Best Practices
Interview Tip
Be prepared to explain the benefits of using enums over magic numbers or strings. Emphasize their role in improving code readability, maintainability, and type safety. Also, be ready to discuss the different types of enums (numeric, string, and heterogeneous).
When to Use Them
Enums are most suitable when you have a predefined set of values that represent distinct states, options, or categories within your application. Examples include:
Memory Footprint
Numeric enums generally have a small memory footprint, as they are represented by integers. String enums, on the other hand, consume more memory due to the storage of string values. However, the trade-off for increased memory usage is improved readability and debuggability with string enums.
Alternatives
Alternatives to enums include:
Pros
Cons
FAQ
-
What is the default value of an enum member if not explicitly assigned?
If not explicitly assigned, enum members are assigned numerical values starting from 0. -
Can I assign string values to enum members?
Yes, you can define string enums where each member is assigned a string literal. This improves readability but may increase memory usage. -
What is a heterogeneous enum?
A heterogeneous enum is an enum where some members have numeric values and others have string values. While supported, it's generally recommended to avoid them as they can lead to confusion.