Implement an enum in JavaScript
This post will discuss how to implement an enum in JavaScript. An enum is a data structure that maps a set of symbolic names to a set of numeric values.
JavaScript does not have a native enum type, but we can simulate one using an object, a function, or a library. To get the integer value from an enum member name in JavaScript, we need to have an data structure that maps the member names to the corresponding values. There are several ways to implement an enum in JavaScript:
1. Using an object literal
We can define our enum as an object literal with key-value pairs, where the keys are the member names and the values are the integer values. To get the integer value from enum member name, we can use the array index or the dot notation. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 |
// Define an enum as an object literal const Color = { RED: 0, GREEN: 1, BLUE: 2 }; // Get the integer value from a member name console.log(Color.RED); // 0 console.log(Color["GREEN"]); // 1 |
We can use Object.freeze() to make our enum object immutable, so that we cannot add, modify, or delete its properties. This can prevent accidental changes to our enum values. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Define an enum as an object literal and freeze it const Color = Object.freeze({ RED: 0, GREEN: 1, BLUE: 2 }); // Get the integer value from a member name console.log(Color.RED); // 0 console.log(Color["GREEN"]); // 1 // Try to change the enum value (will fail silently or throw an error in strict mode) Color.RED = 3; console.log(Color.RED); // still 0 |
2. Using a function
We can use a function to create and return our enum object, and optionally use a closure to hide the implementation details. This can give we more flexibility and control over how we define and access our enum values. To get the corresponding value from enum member name, we can array index or the dot notation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Define an enum as a function that returns an object function Color() { // Declare a private variable to store the enum object let _enum = { RED: 0, GREEN: 1, BLUE: 2 }; // Return the enum object return _enum; } // Get the integer value from a member name console.log(Color().RED); // 0 console.log(Color()["GREEN"]); // 1 |
3. Using ES6 Symbols
We can also use ES6 Symbols to create unique identifiers that cannot be duplicated and can be used to define constant values that are guaranteed to be unique and immutable. We can use Symbol() to create a symbol value, and assign it to a property of an object. For instance:
|
1 2 3 4 5 6 7 8 |
const direction = Object.freeze({ UP: Symbol('Up'), DOWN: Symbol('Down'), LEFT: Symbol('Left'), RIGHT: Symbol('Right') }); console.log(direction.UP); // Symbol(Up) |
4. Using TypeScript
TypeScript is a superset of JavaScript that includes support for enums. To implement an enum using TypeScript, we can use the enum keyword followed by the name of the enum and a set of constant values enclosed in curly braces. For instance:
|
1 2 3 4 5 6 |
enum Direction { Up, Down, Left, Right } |
That’s all about implementing an enum in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)