What Is Enum in JavaScript? Real Usage

How-to basics · Updated November 23, 2026

In JavaScript, enum is not a native data type. The word “enum” is a programming term borrowed from languages like C, C++, and Java, where it defines a set of named constants. JavaScript does not have a built-in enum keyword (as of ECMAScript 2024), so developers simulate enums using objects, frozen objects, or TypeScript’s enum feature. This article explains what an enum is, why JavaScript lacks it, and how to create reliable enum-like structures in your code.

Origin of the Term “Enum”

The term “enum” comes from the C programming language, introduced in the 1970s as a way to declare a group of integer constants with descriptive names. For example, in C you might write:

enum Color { RED, GREEN, BLUE };

This creates a new type Color with three possible values. The word itself is short for “enumeration,” meaning a list of named items. Over time, languages like Java, C#, and Python adopted similar constructs. JavaScript, however, was designed with a different type system—it has no explicit enum type, but developers often use objects to mimic the behavior.

Why JavaScript Doesn’t Have a Native Enum

JavaScript’s design philosophy emphasizes simplicity and flexibility. The language uses dynamic typing and prototype-based inheritance, which makes a rigid enum type less necessary. Instead, you can use plain objects, symbols, or even strings to represent a fixed set of values. The ECMAScript specification has considered adding enums, but as of 2024, no proposal has reached the final stage. This means the word “enum” is nonstandard in JavaScript—it’s not a keyword, and using it as a variable name is allowed but not recommended because it might confuse readers.

How to Simulate an Enum in JavaScript

The most common pattern is to use a plain object with Object.freeze() to prevent modifications. Here’s a typical example:

const Color = Object.freeze({
  RED: 'red',
  GREEN: 'green',
  BLUE: 'blue'
});

This creates an object with three properties. Using Object.freeze() makes the object immutable, so you can’t accidentally change Color.RED to something else. You can also use a simple object without freezing, but that allows accidental reassignment. Another approach is to use Symbol values to guarantee uniqueness:

const Color = Object.freeze({
  RED: Symbol('red'),
  GREEN: Symbol('green'),
  BLUE: Symbol('blue')
});

Symbols ensure that each value is unique, even if two enums have the same string name. This is useful when you want to avoid string collisions.

TypeScript’s Enum: The Closest Thing

If you use TypeScript, a superset of JavaScript, you get a real enum keyword. TypeScript compiles to JavaScript, and its enums are a compile-time feature. For example:

enum Color { Red, Green, Blue }
let c: Color = Color.Green;

TypeScript enums can be numeric or string-based. Numeric enums assign incrementing numbers by default (0, 1, 2), while string enums require explicit values. When compiled, TypeScript generates an object similar to the manual pattern above. This is the only official way to use the word “enum” as a keyword in the JavaScript ecosystem, but it’s not standard JavaScript—it’s a TypeScript extension.

Common Problems and Misconceptions

A frequent misconception is that enum is a reserved word in JavaScript. It is not. You can actually declare a variable named enum:

let enum = 'hello'; // works, but bad practice

However, doing so is confusing because it clashes with the common meaning. Another problem is that plain objects are mutable by default. If you don’t freeze them, another part of your code could overwrite a value, leading to bugs. Also, when using string-based enums, you might accidentally compare a value to the wrong string. Using Symbol helps avoid that, but symbols don’t serialize to JSON nicely, so if you need to send enum values over the network, strings are often easier.

How to Choose the Right Enum Pattern

When deciding how to implement an enum in JavaScript, consider your use case:

  • Simple string values: Use a frozen object with string values. This is readable and works well with JSON.
  • Need uniqueness: Use Symbol values to prevent accidental collisions, but be aware that symbols don’t stringify.
  • Type safety: Use TypeScript enums if you’re in a TypeScript project. They provide compile-time checks and autocompletion.
  • Performance: Plain objects are fast; symbols are slightly slower due to their internal representation, but the difference is negligible for most apps.

For most JavaScript projects, a frozen object with string values is the simplest and most maintainable choice. It’s easy to read, debug, and serialize.

Alternatives to Enums

Besides objects and symbols, you can use arrays or sets to represent a fixed set of values. For example, you could define an array of allowed values and check membership:

const COLORS = ['red', 'green', 'blue'];
if (COLORS.includes(color)) { ... }

This is useful for validation. Another alternative is to use a Map if you need to associate extra data with each enum value. However, these alternatives lack the named-property convenience of an object. In modern JavaScript, you can also use Object.groupBy() or other array methods, but they don’t replace the concept of an enum.

Frequently Asked Questions

Is “enum” a reserved word in JavaScript?

No, “enum” is not a reserved word in JavaScript. It is a future reserved word in some older specifications, but in ECMAScript 5 and later, it is not reserved. You can use it as a variable name, but it’s discouraged because it’s not a standard construct.

Can I use TypeScript enums in plain JavaScript?

No, TypeScript enums are a TypeScript feature. When you compile TypeScript to JavaScript, the enum is converted to an object. You cannot use the enum keyword directly in plain JavaScript files.

What is the difference between an enum and a constant object?

An enum is a language-level construct that provides type safety and a fixed set of values. A constant object is just a JavaScript object that you freeze. In JavaScript, there is no compile-time type checking, so the object is just a convention. The term “enum” is often used informally to refer to such objects.

How do I make an enum immutable in JavaScript?

Use Object.freeze() on the object. This prevents adding, removing, or changing properties. For example: const Color = Object.freeze({ RED: 'red', GREEN: 'green' });. Note that freezing is shallow, so if your values are objects, they can still be mutated.

Should I use strings or symbols for enum values?

Strings are easier to debug and serialize. Symbols guarantee uniqueness but are not serializable to JSON. If you need to send enum values over an API, use strings. If you need to avoid accidental string collisions, use symbols.

Why does JavaScript not have a native enum?

The ECMAScript committee has considered enums but hasn’t finalized a proposal. JavaScript’s flexibility allows developers to create enum-like structures easily, so a built-in enum is not a high priority. TypeScript fills this gap for developers who want stricter typing.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.