javascript enum type 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: enum javascript
const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)
Example 3: declare enum in type script
enum PrintMedia {
Newspaper = 1,
Newsletter,
Magazine,
Book
}
Example 4: typescript enum value to enum
enum Number {
One,
Two
}
const numberOne: Number = "One" as Number;
const otherNumberOne: Number = Number.One;
const stringOne = Number[Number.One];