enum in 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: ts enum definition
enum PrintMedia {
Newspaper,
Newsletter,
Magazine,
Book
}
Example 3: enum javascript
const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)
Example 4: ts enum
enum Response {
No = 0,
Yes = 1,
}
function respond(recipient: string, message: Response): void {
}
respond("Princess Caroline", Response.Yes)