Assigning values to enum values outside enum declaration can be achieved by using a technique called “enum extension.” This allows us to assign custom values to each enum value, providing more flexibility in representing data.
By default, enum values are automatically assigned sequential integer values starting from 0. However, in certain situations, it might be necessary to assign different or specific values to enum members. Here’s how you can assign custom values to enum values outside the enum declaration:
enum Colors {
Red,
Blue = 5,
Green,
Purple = 10,
Yellow
}
In the example above, the enum Colors
has five members: Red
, Blue
, Green
, Purple
, and Yellow
. By explicitly assigning values to some of the members, we can control their assigned values, resulting in a non-sequential sequence.
The value Red
is assigned 0 by default since we didn’t explicitly assign it a value. However, we assigned 5 to Blue
, which makes it have a value of 5. Green
comes next with a value of 6 (since enum values increment by 1), and subsequently, Purple
has a value of 10. Finally, Yellow
gets assigned 11.
It’s essential to note that once a custom value is assigned to an enum member, the subsequent members continue with sequential values from that point.
Here’s an example demonstrating the usage of the Colors
enum:
let color: Colors = Colors.Green;
console.log(color); // Output: 6
The above code snippet assigns the value Green
to the variable color
of type Colors
. When we print the value of color
, it displays 6, confirming that the assigned value of Green
is indeed 6.
Related FAQs:
1. Can you assign any type of value to enum members?
No, enum members can only be assigned values of type number or string.
2. Is it mandatory to assign values to all enum members if we assign to one of them?
No, it is not necessary to assign values to all enum members if you explicitly assign a value to at least one of them. The subsequent members will receive sequential values starting from that point.
3. Can we assign the same value to multiple enum members?
No, each enum member must have a unique value. Assigning the same value to multiple members will result in a compilation error.
4. Can we assign non-sequential values manually to enum members?
Yes, by explicitly assigning values to enum members, you can achieve non-sequential sequences. However, it is generally advisable to stick to sequential or patterned values to maintain code readability.
5. Can we assign values to enum members during runtime?
No, enum members’ values must be assigned during compile-time. Values assigned to enum members cannot be modified once the program is running.
6. Can we assign decimal or floating-point values to enum members?
No, enum members can only have values of type number or string. Decimal or floating-point values are not allowed.
7. Can we assign negative values to enum members?
Yes, negative values can be assigned to enum members of type number, as long as they fall within the range of valid numbers.
8. Do enum members have to begin with a value of 0?
No, enum members can have any assigned value. Starting from 0 is the default behavior, but it can be overridden for individual members.
9. Are enum values limited to integers?
No, enum members can also be assigned string values. For example, we could assign "Primary"
to the Colors.Red
enum member.
10. What happens if we don’t assign a value to any enum members?
If a value is not explicitly assigned to an enum member, it will be automatically assigned the next sequential value based on the preceding members.
11. Can the value assignment of enum members be skipped?
Yes, you can skip values while assigning custom values to enum members. For example, you can assign values of 0, 5, and 10 to three members and let the remaining members receive auto-assigned sequential values starting from 11.
12. Can we use enum members as keys in objects?
Yes, enum members can be used as keys in objects. This can be useful in scenarios where you want to associate specific values or behaviors with each enum member.