The enum type is also called an enumeration type. It is a type that can group multiple constants into one and append a series of values. The constants defined using enumerations are called enumerator lists. By default, the enumeration Lifters are numbered sequentially starting from zero. This article introduces you to the use of enumeration types in JavaScript.
What is enum (enumeration type) in JavaScript?
There is no enumeration type in JavaScript, except JavaScript All languages have the enum keyword, but in order to use enumeration variables in JavaScript, we have to create it ourselves.
Let’s take a look at How to define enum (enumeration type) in JavaScript
Let’s take a look at a specific example of defining enum (enumeration type) in JavaScript
The code is as follows
var Fruit = { orange : 1, banana : 2, peach : 3, strawberry : 4 }; var myvar = Fruit.orange; if (myvar == 1){ console.log("It is an orange!"); } else { console.log("It is NOT an orange"); }
The execution result is as follows
In the above code, we first create a A dictionary variable named Fruit.
Multiple enumerators are set in the Fruit variable and their integer values are given respectively.
Then we use the operator to store the value of the orange enumerator in the variable myvar.
If the value of myvar is 1, display It is an orange! in the JavaScript console, otherwise display It is NOT an orange!.
Finally, in this case, myvar has a value of 1, so It is an orange! is displayed.
The above is the detailed content of How to define enum (enumeration type) in JavaScript? how to use?. For more information, please follow other related articles on the PHP Chinese website!