enum of values js code example
Example 1: javascript enum
const seasons = {
SUMMER: {
BEGINNING: "summer.beginning",
ENDING: "summer.ending"
},
WINTER: "winter",
SPRING: "spring",
AUTUMN: "autumn"
};
let season = seasons.SUMMER.BEGINNING;
if (!season) {
throw new Error("Season is not defined");
}
switch (season) {
case seasons.SUMMER.BEGINNING:
case seasons.SUMMER.ENDING:
case seasons.SUMMER:
case seasons.WINTER:
case seasons.SPRING:
case seasons.AUTUMN:
}
Example 2: typescript enum to string
enum AnEnum {
One = 1,
Two = 2
}
let stringOne = AnEnum[1];
let stringTwo = AnEnum[AnEnum.Two];