An enum is a special "class" that represents a group of constants (unchangeable variables).
Enums come in two flavors
numeric. Lets start with numeric.
Numeric Enums - Default
By default, enums will initialize the first value to
0 and add 1 to each additional value:
let currentDirection = CardinalDirections.North;
// logs 0
// throws error as 'North' is not a valid enum
currentDirection = 'North'; // Error: "North" is not assignable to type 'CardinalDirections'.
Numeric Enums - Initialized
You can set the value of the first numeric enum and have it auto increment from that:
North = 1,
// logs 1
// logs 4
Numeric Enums - Fully Initialized
You can assign unique number values for each enum value. Then the values will not incremented automatically:
NotFound = 404,
Success = 200,
Accepted = 202,
BadRequest = 400
// logs 404
// logs 200
Enums can also contain
strings. This is more common than numeric enums, because of their readability and intent.
North = 'North',
East = "East",
South = "South",
West = "West"
// logs "North"
// logs "West"
Technically, you can mix and match string and numeric enum values, but it is recommended not to do so.