TIL using Object.freeze to simulate enums in Javascript
POSTED ON:
TAGS: javascript datatype objects
Enums are useful to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.
Javascript doesn't have native support for Enums.
But you can simulate it with Object.freeze
const Direction = Object.freeze{
Up: 'Up',
Down: 'Down',
Left: 'Left',
Right: 'Right'
};
Via 3 TypeScript Tricks You Can Use in JavaScript
Related TILs
Tagged: javascript