0
0
Typescriptprogramming~10 mins

Numeric enums in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Numeric enums
Start
Define enum with numeric values
Assign default or explicit numbers
Use enum values in code
Access enum by name or number
End
This flow shows how numeric enums are defined, assigned values, and accessed in TypeScript.
Execution Sample
Typescript
enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}
console.log(Direction.Up, Direction.Down, Direction.Left, Direction.Right);
Defines a numeric enum Direction with explicit and implicit values, then prints all values.
Execution Table
StepActionEvaluationResult
1Define enum Direction with Up=1Up = 1Direction.Up = 1
2Assign Down without valueDown = Up + 1Direction.Down = 2
3Assign Left without valueLeft = Down + 1Direction.Left = 3
4Assign Right without valueRight = Left + 1Direction.Right = 4
5Print Direction.UpDirection.Up1
6Print Direction.DownDirection.Down2
7Print Direction.LeftDirection.Left3
8Print Direction.RightDirection.Right4
9End of execution--
💡 All enum members assigned numeric values; printing completes.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Direction.Up-11111
Direction.Down--2222
Direction.Left---333
Direction.Right----44
Key Moments - 2 Insights
Why does Down get the value 2 even though we didn't assign it explicitly?
Because in numeric enums, if a member is not assigned a value, it gets the previous member's value plus one, as shown in execution_table step 2.
Can we access enum members by their numeric values?
Yes, TypeScript enums create a reverse mapping, so you can access the name by number, but this example focuses on numeric values only.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of Direction.Left after step 3?
A3
B2
C4
D1
💡 Hint
Check the 'Result' column in row with Step 3 for Direction.Left.
At which step does Direction.Right get its value assigned?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Action' column for assignment of Direction.Right.
If we changed Up to 5, what would Direction.Down be after step 2?
A5
B1
C6
D4
💡 Hint
Remember that unassigned members get previous value plus one, see variable_tracker.
Concept Snapshot
Numeric enums assign numbers to names.
Start with explicit or default 0.
Unassigned members get previous + 1.
Access by name or number.
Useful for readable constants.
Full Transcript
This example shows how TypeScript numeric enums work. We define an enum Direction with Up assigned 1 explicitly. The next members Down, Left, and Right get values 2, 3, and 4 automatically because they follow Up. We print all values to see the numbers. Numeric enums let us use names instead of numbers, making code easier to read. Unassigned members get the previous member's value plus one. This is shown step-by-step in the execution table and variable tracker. Understanding this helps avoid confusion about enum values.