enum 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];
Example 3: enum javascript
const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)
Example 4: enum in ts
enum Direction {
Up,
Down,
Left,
Right,
}
Example 5: ts enum
enum Response {
No = 0,
Yes = 1,
}
function respond(recipient: string, message: Response): void {
}
respond("Princess Caroline", Response.Yes)