What is a numeric enum, and what values do its members get by default?
Short answer
A numeric enum assigns sequential integer values starting at 0 to each member by default, unless you explicitly assign a starting value — subsequent members then auto-increment from there.
Advertisement
AModel answer
A numeric enum assigns sequential integer values starting at 0 to each member by default, unless you explicitly assign a starting value — subsequent members then auto-increment from there.
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
console.log(Direction.Up); // 0Real-world example
Representing a fixed set of directions, statuses, or days of the week with meaningful names instead of magic numbers.
Likely follow-ups
- What happens if you assign a custom starting value to the first member, like Up = 1?
Related topics