Enum With and Without Values

In this lesson, you will discover how to use an enum with explicit and implicit values.

The role of enum #

An enum is a structure that proposes several allowed values for a variable. It is a way to constrain variable values by defining specific possible entries.

enum with values

enum can be of string type. In that case, every member requires a value without exception.

Press + to interact
enum MyStringEnum {
ChoiceA = "A",
ChoiceB = "B",
}

A mixed enum value type is acceptable if every member is defined. For example, you can have one item be an integer and another be a string type. It is recommended not to mix types since it might be more confusing than pragmatic.

Press + to interact
enum MyStringAndNumberEnum {
ChoiceA, // 0
ChoiceB = "B",
ChoiceC = 100
}

enum without values

enum is a type that enforces a limited and defined group of constants. enum ...