Enums are for named constants
Enums are for named constants#
Enums are used to represent sets of named constants. They improve the readability of our code and provide a means for defining numeric and string constants. Unlike the features covered so far, enums can be used as both type expressions and runtime expressions, meaning enums have a real value at runtime.
In the example below, we represent HTTP status codes using an
logStatus() function accepts an
HTTP member and prints a corresponding message depending on the given status code. When calling
logStatus(), we have to use an
HTTP enum member as the argument: