Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

What is the preferred syntax for defining enums in JavaScript? Something like:

my.namespace.ColorEnum = {

RED : 0, 

GREEN : 1, 

BLUE : 2 

}

// later on 

if(currentColor == my.namespace.ColorEnum.RED) { 

// whatever

}

Or is there a more preferable idiom?

1 Answer

0 votes
by (106k points)

The preferred syntax for defining enums in JavaScript is to use the objects see the code below:-

var DaysEnum = Object.freeze({"monday":1, "tuesday":2, "wednesday":3, ...})

or

var DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...} Object.freeze(DaysEnum)

Related questions

0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...