Enum is used to define a human-readable name for a specify number or string. The method to use an enum is to put its value into the switch statement. The below program will determine whether a light is on or off using Enum.
enum LightSwitch { On, Off }
The above enum has two values, the on and off state for a light switch.
function lightOnOff(state : LightSwitch) { switch(state) { case LightSwitch.On: console.log("Light's On!") break case LightSwitch.Off: console.log("Light's Off!") break } } lightOnOff(LightSwitch.On) /* Light's On! */ lightOnOff(LightSwitch.Off) /* Light's Off! */
As the user entered the type LightSwitch enum value (on or off) into the above function, the switch statement will print the state of the light switch accordingly.