JavaScript enum: The Complete Guide

JavaScript enums are types that contain only a limited number of fixed values. Javascript does not support enum natively. However, we can use an enum when needing a predefined list of values representing the string or numeric data. It allows us to store predefined named constants. But we can create an enum by using the Object.freeze() method. So, we only can read them and can not update, read or delete them.

We can use an enum when we need a predefined list of values representing the string or numeric data. It allows us to store predefined named constants. Unfortunately, Javascript does not support enum natively. But we can create an enum by using the Object.freeze() method. So we only can read them and can not update, read or delete them.

Syntax

Object.freeze(enum_object);

Parameters

It takes an object to freeze.

Example

const colorMeansEnum = {
 RED: "Danger Zone",
 GREEN: "Safe Zone",
 YELLOW: "Medium Zone",
 BLACK: "Invisible Zone"
};

Object.freeze(colorMeansEnum);
console.log(colorMeansEnum.RED)
console.log(colorMeansEnum.BLACK)

Output

Danger Zone
Invisible Zone

The above example is the simplest way to create an enum. First, we create a colorMeans Enum that describes the different colors and zones. Using Object.freeze(), we can not now update, delete, or add any property in this enum.

We can only read this enum when using object property in our program. For example, Object.freeze() does not freeze objects nested so that we can freeze them too.

Javascript enum using class

In this example, we create a simple enum class. It requires only a constructor having a variable number of arg. It adds a key to every enum, freezes them, and then creates an instance. For making enum iterable and more valuable, we use the Symbol.iterator.

class RollNoEnum {
    constructor(...keys) {
      keys.forEach((key, i) => {
      
      // Roll No starting from 1
      this[key] = i + 1;
     });
    
    Object.freeze(this);
 }

   *[Symbol.iterator]() {
      for (let key of Object.keys(this)) {
      yield key;
   }
  }
}

const studentEnum = new RollNoEnum(
 'dhruv',
 'yash',
 'nidhi',
 'charmi',
 'aryan',
 'prakash'
);

const days = [...studentEnum];

console.log(days) 

Output

[ 'dhruv', 'yash', 'nidhi', 'charmi', 'aryan', 'prakash' ]

In this example, we created one RollNoEnum class which takes studentEnum and stores keys them starting by 1 to student length. And then we use the symbol iterator so we can iterate that enum. We can see in the last console.log(), that we get an array of studentEnom in string. So it’s best to create an enum through the class constructor and for iterating use the Symbol.iterator.

Best Use Case of enum class

If we are working as a team developer, we need to define some constant enumThat Everyone reads that enum but no one can delete, update, or add some value in this enum so it’s the best way to use it in this type of case.

As we discussed, JavaScript does not support enum natively but we can create it by the Object.freeze(). It can save inline time and compile time in javascript. It can improve the efficiency of JavaScript programs.

That’s it for this tutorial.

See also

JavaScript eval()

Write Comment in JavaScript

Leave a Comment