DEV Community

Discussion on: Enums in JavaScript

Collapse
 
marcosmol204 profile image
Marcos Molina 👩🏽‍💻

Can you attach some example :)?

Collapse
 
zhengmao profile image
mao.zheng

it's just like this:

const Color = Object.freeze({
    RED  :  Symbol("red"),
    BLUE :  Symbol("blue"),
    GREEN:  Symbol("green")
});
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
marcosmol204 profile image
Marcos Molina 👩🏽‍💻

Why do you prefer? Could you explain ?

Thread Thread
 
zhengmao profile image
mao.zheng

Because this will force you to use the enum object defined above. you can't just use string(number) literal to make a comparison. It keeps the consistency of the codes your wrote

Thread Thread
 
marcosmol204 profile image
Marcos Molina 👩🏽‍💻

Can you write an example ?
Using the enum, checking if a color is a valid color in the enum ?