Open In App

Convert a JavaScript Enum to a String

Last Updated : 30 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Enums in JavaScript are used to represent a fixed set of named values. In JavaScript, Enumerations or Enums are used to represent a fixed set of named values. However, Enums are not native to JavaScript, so they are usually implemented using objects or frozen arrays. In this article, we are going to explore various approaches to converting a JavaScript enum to a string representation.

These are the following ways to convert an enum to a string:

Approach 1: Using Object Key

In this approach, we’ll use the fact that object keys are strings. Each enum value corresponds to a key in the object. We can use the enum value as a key to retrieve its corresponding string representation.

Syntax:

const enumObject = {
VALUE1: 'String1',
VALUE2: 'String2',
// ... other enum values
};
const stringValue = enumObject[enumValue];

Example: In this example, we are using the object keys, to convert a specific enum value to its corresponding string representation

Javascript




const status = {
    SUCCESS: 'Operation Successful',
    ERROR: 'Operation Failed',
    PENDING: 'Operation Pending',
};
 
const currentStatus = 'ERROR';
const statusString = status[currentStatus];
console.log(statusString);


Output

Operation Failed

Approach 2: Using a Switch Statement

In this approach, We switch on the enum value and return the corresponding string.

Syntax:

function enumToString(enumValue) {
switch (enumValue) {
case ENUM.VALUE1:
return 'String1';
case ENUM.VALUE2:
return 'String2';
// ... other cases
default:
return 'DefaultString';
}
}

Example: In this example, the switch statement is used to convert a given enum value to its associated string.

Javascript




const ENUM = {
    VALUE1: 'A',
    VALUE2: 'B',
};
 
function enumToString(enumValue) {
    switch (enumValue) {
        case ENUM.VALUE1:
            return 'String1';
        case ENUM.VALUE2:
            return 'String2';
        default:
            return 'DefaultString';
    }
}
 
const currentEnumValue = ENUM.VALUE1;
const stringValue = enumToString(currentEnumValue);
console.log(stringValue);


Output

String1

Approach 3: Using Map

In this approach, We are using a Map data structure where keys are enum values, and values are their string representations.

Syntax:

const enumMap = new Map([
[ENUM.VALUE1, 'String1'],
[ENUM.VALUE2, 'String2'],
// ... other enum mappings
]);
const stringValue = enumMap.get(enumValue);

Example: In this example, we have used the Map data structure to establish a direct mapping between enum values and their corresponding string representations.

Javascript




const OPERATION = {
    ADD: 'Addition',
    SUBTRACT: 'Subtraction',
};
 
const operationMap = new Map([
    [OPERATION.ADD, 'Addition'],
    [OPERATION.SUBTRACT, 'Subtraction'],
]);
 
const currentOperation = OPERATION.ADD;
const operationString = operationMap.get(currentOperation);
console.log(operationString);


Output

Addition


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads