Home Web Front-end JS Tutorial How to define enum (enumeration type) in JavaScript? how to use?

How to define enum (enumeration type) in JavaScript? how to use?

Jan 07, 2019 pm 04:00 PM
enumeration type

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.

How to define enum (enumeration type) in JavaScript? how to use?

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");
}
Copy after login

The execution result is as follows

How to define enum (enumeration type) in JavaScript? how to use?

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Unknown USB device, device enumeration bug fix on Windows 11/10 Unknown USB device, device enumeration bug fix on Windows 11/10 Apr 18, 2023 pm 10:13 PM

After using USB devices for a long time, we all know that when you plug the USB device into your Windows PC, first install the driver required for the USB, and then you can see the USB device in File Explorer. Can be accessed. Whenever any hardware device is connected to a Windows PC, a driver is required to help communicate with the device. However, if the driver is corrupted, Windows will not recognize the hardware device. One such error that occurs while using USB devices on Windows computers is UnknownUSBDevice(DeviceFailedEnumeration)Error. USB

How to use the enum module to define enumeration types in Python 2.x How to use the enum module to define enumeration types in Python 2.x Jul 29, 2023 pm 09:33 PM

How to use the enum module to define enumeration types in Python2.x Introduction: An enumeration is a data type that limits the value of a variable to a limited range. Using enumeration types can make the code clearer and more readable. In Python2.x, we can use the enum module to define enumeration types. This article will introduce how to use the enum module to define and use enumeration types, and give corresponding code examples. Importing the enum module Before using the enum module, you first need to import the module. exist

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

Enumeration types in Java can be mapped to enumeration types in the database and are used to represent status, permissions or roles to maintain data integrity. Specific application scenarios include: indicating order status, such as creation, processing, delivery, etc. Indicates user permissions or roles, such as administrator, user, guest, etc. Used to limit user input data and ensure data consistency, such as the type of post is discussion, question or answer, etc.

How to convert JSON object to enum type in Java using Jackson? How to convert JSON object to enum type in Java using Jackson? Sep 05, 2023 pm 12:13 PM

JSONObject can parse text in a string to generate a Map type object. Enumerations can be used to define collections of constants, and we can use enumerations when we need a predefined list of values ​​that does not represent some kind of numeric or textual data. We can convert JSON objects into enumerations using the readValue() method of the ObjectMapper class. In the example below, we can use the Jackson library to convert/deserialize a JSON object into a Java enumeration. Example importcom.fasterxml.jackson.databind.*;publicclassJSONToEnumTest{&

How to assign values ​​using enumeration types in Java How to assign values ​​using enumeration types in Java Jan 31, 2024 pm 06:33 PM

What is an enumeration type? An enumeration type (enum) is a special data type in the Java programming language that is used to represent a set of predefined constants. Each constant in an enumeration type represents a possible value of that type. How to set value using enum type? To set a value using an enumeration type, you can use a constant of the enumeration type. Constants of enumeration types can be accessed through the dot operator (.). For example, if there is an enumeration type called Color, which contains three constants: RED, GREEN, and BLUE

What is the difference between Java enumeration types and interfaces? What is the difference between Java enumeration types and interfaces? May 01, 2024 am 10:42 AM

An enumeration type is a collection of fixed values ​​and cannot be inherited, but member methods and variables can be defined. The interface defines a set of methods and constants, which cannot be instantiated, but can be implemented by the class. The methods of the interface can only be declared but cannot be implemented, but constants can be defined.

What is the role of Java enumeration types in secure programming? What is the role of Java enumeration types in secure programming? Apr 30, 2024 pm 12:15 PM

Enhancing safe coding with Java enumeration types enables: type safety, ensuring that only defined values ​​are used. It is highly readable and uses constant names to represent values, making it easy to understand. To prevent illegal input, limit the value to only the value in the enumeration. Security programming applications include user permission definition and verification.

Learn the basics of Java enumeration type enum Learn the basics of Java enumeration type enum Feb 01, 2024 am 09:16 AM

Introduction to the basic usage of Java enumeration type enum 1. Definition of enumeration type An enumeration type (enum) is a type in the Java programming language that allows you to create a set of constants with fixed values. Enumeration types are similar to classes in Java, but they have some key differences. First, enumeration types are final, which means they cannot be inherited. Secondly, an enumeration type can only have one instance, which means you cannot create multiple objects of the enumeration type. The enumeration type is defined as follows: enumMyEnu

See all articles