Enum in JS

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Enum is a data type in programming languages that defines a set of constant values. It is a distinct value type (i.e.) all values in an enum are unique. Using an enum makes the code readable and maintainable. Some examples of enum are all weekdays from Monday to Sunday, T-shirt sizes like S, M, L, XL, etc.

The pseudocode of an enum variable can look like below:

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Does JavaScript Support Enum ?

Even though enum data type is widely used in many programming languages like C++, Java, etc., Javascript doesn't have a built-in enum data type. Enum's functionalities can be implemented in Javascript using classes and objects.

Ways to Implement Enum in Javascript

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Enum Using Objects

Enums can be implemented in Javascript using objects. The object can have key-value pairs where the key can be treated as the enum value, and the matter can be anything.

Syntax :

Example :

Let's use a Javascript object to implement an enum for the directions such as north, east, west, and south.

One problem with the above implementation is that the values in the Direction object are mutable. Since enums are immutable, we can achieve immutability by freezing the object using the Object.freeze() method.

The Object.freeze() method freezes an object meaning new properties cannot be added, existing properties, and the object's prototype cannot be re-assigned.

The values can be accessed line below :


Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Enum Using Symbols

One problem we see with the above approach is collision (i.e.) the value of one object may collide with the value of the other object. Let's understand it with the enums Direction and Pole.

The output of the above code will be :

Even though the North direction and North pole are different enum instances, they are treated equally. Symbol can be used to overcome this issue.

Symbol is a built-in object whose constructor returns a value guaranteed to be unique. Let's use symbols to implement the Direction and Pole enum.

The output of the above code is :

The above code returns false because the values of the Direction instance and Pole instance are different, as Symbol returns unique values. Use this Online JavaScript Formatter to format your code.


Enum Using Classes

Enums can be implemented using Javascript classes, where each value will be the static instance of the class itself but with different values.

Let's understand it with an example :

Example :

Let's implement the Direction enum using Javascript classes.

The enum value can be accessed like below :

Output :

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

When to Use Enums in Javascript ?

Enums can be used in Javascript when it provides the below advantages :

  • Improves the readability and maintainability of the code.
  • Reduces errors caused by mistyping.
  • Useful when there is a fixed number of values like days of the week, months of the year, etc.

Learn About Important Concepts in JavaScript

Please visit Scaler Topics' Javascript hub to learn more about Javascript

Conclusion

  • Enum is a data type in programming languages that defines a set of constant values.
  • All values in an enum should be unique.
  • Although the enum data type is widely used in many programming languages, Javascript doesn't have the enum data type.
  • Enum can be implemented using objects, classes, and symbols in Javascript.
  • Symbol is a built-in object whose constructor returns a value that is guaranteed to be unique
  • Symbol avoids collision between two enums with the same values.