Updated March 29, 2023
Introduction to ES6 Enum
ES6 provides different types of features to the user, in which that enum is one of the features that are provided by the ES6. Basically, enum allows the developer to define the set of name constants that the developer wants as per their requirement. Using enum features, we can easily handle the intent or create a set of different document cases as per our requirements. Normally TypeScript is not a type-level extension of JavaScript, but TypeScript provides the two types of enum such as numeric and string-based enums. Therefore, the implementation of the enum is simple, and we can easily adapt it in our coding as per our requirements.
What is ES6 enum?
In programming, an enum type (additionally called specification, enum, or factor in the R programming language and a clear cut variable in measurements) consists of a bunch of named values called components, individuals, enumerate, or enumerators of the kind. The enumerator names are typically identifiers that act as constants in the language. A counted type can be viewed as a ruffian labeled association of unit type. A variable that has been pronounced as having a listed kind can be relegated to any of the enumerators as worth. As such, a listed kind has values that are not the same as one another and can be measured up and allocated, yet are not indicated by the developer as having a specific substantial portrayal in the PC’s memory; compilers and translators can address them self-assertively.
To put it momentarily, enums are an extraordinary sort of “shut” class, every one of the occurrences of which is distinct at the arranged time and available through the actual class (commonly as static factors, for example, Enum.INSTANCE). They’re helpful for demonstrating a sort whose examples are predefined and (normally) consistent — permitting better sort requirement and meaningfulness.
For instance, CSS properties, for example, ‘position’ or ‘show,’ steady arrangements of items like the planets in the planetary group, and numerous semantic ideas (tenses, for instance) can be appropriately demonstrated utilizing enum types, and the rundown continues; really, numerous applications have consistent articles or substances which can be overhauled through utilizing enums.
Likewise, explicitly in Javascript — where there’s no local execution of enum types, and to reproduce them, we really want to utilize progressed designs and elements of the language — figuring out how to reproduce progressed enums as an example has high instructive worth, as it uncovered a few capacities that engineers probably won’t run over regularly.
Numeric enums
First, we start with numeric enums, which are presumably more natural on the off chance that you’re coming from different dialects. An enum can be characterized by utilizing the enum catchphrase.
String enums
String enums are comparable; however, they have some inconspicuous runtime contrasts as reported beneath. Every part must be consistently instated with a string exacting or with another string enum part in a string enum.
How to use ES6 enum?
Now let’s see how we can use the ES6 enum as follows.
Basically, enum is not supported in JavaScript. We can anyway make Enums utilizing Object.freeze by making objects containing every one of the enumerable properties and afterward freezing the article so that no new enum can be added to it.
const Fruits = {
Apple: 2,
Banana: 3,
Mango: 1,
Orange: 4
};
Object.freeze(Fruits);
let R_Apple = Fruits.Apple;
Fruits.Pomegranate= 4; // try to add new fruits
console.log(Fruits)
Explanation
By using the above code, we can implement enum; in this example, we use enums to create object.freeze, and here we create. After that, we try to add new fruits into the const, but it fails.
Creating ES6 enum
Now let’s see how we can create an ES6 enum as follows.
In making progressed Enum types in Typescript, we utilized Typescript’s convenient read-only and private access modifiers to make an enum design with impressive adaptability and progressed highlights. The following sensible advance is to structure an equal example for enums in Javascript — for situations where a Javascript climate is liked or required.
The catch here is that Javascript, and all the more explicitly Javascript ES5 and ES6 — the most well-known objective renditions — have no help for read-only properties and most certainly not for private accessors. So to execute an enum design here, we’ll need to utilize further, maybe not as normal devices or examples. What’s more, that is not all that awful! a great deal can be gained from simply messing with those, as we will do soon. To be more exact, we’ll utilize some less continuous strategies for the Object class and, in ES6, the Proxy API, as well.
The creation of the enum class depends on the programming language as follows.
Suppose we are using Pascal programming language, then we need to use the following syntax as follows.
var car(Automatic, Manual);
Now let’s suppose we need to create an enum in c programming. At that time, we need to use the following example as follows.
enum carsuit{Color, type, cost, model};
struct car {
enum carsuit ;
}
Enm carsuit;
So in this way, we can create enum in documents as per our requirement and structure; syntax depends on the programming language.
ES6 enum code example
Now let’s see a different example of ES6 enum for better understanding as follows.
const Fruits = {
Apple: 2,
Banana: 3,
Mango: 1,
Orange: 4
};
Object.freeze(Fruits);
let R_Apple = Fruits.Apple;
Fruits.Pomegranate= 4; // try to add new fruits
console.log(Fruits)
Explanation
In the above example, we try to implement the enum; here, first, we need to create the const of Fruits as shown. In this example, we use the object.freeze to create the enum, and additionally, here, we try to add one more fruit, but it fails to add. We illustrated the final output of the above code implementation using the following screenshot as follows.
Conclusion
We hope from this article you learn more about the ES6 enum. From the above article, we have taken in the essential idea of the ES6 enum, and we also see the representation and example of the ES6 enum. Furthermore, we learned how and when we use the ES6 enum from this article.
Recommended Articles
This is a guide to ES6 Enum. Here we discuss the essential idea of the ES6 enum, and we also see the representation and example of the ES6 enum. You may also have a look at the following articles to learn more –