HOWTO · JavaScript

Enumerator in JavaScript

Learn two enum-like patterns in JavaScript: ordered arrays with indexOf and objects built from names.

On this page

JavaScript has no traditional built-in Enum type. In plain JavaScript, you can use an ordered array when a position matters, or build an object that maps names to numeric values. For array lookups, indexOf(searchElement) searches for a matching value. TypeScript does provide enum syntax, but these JavaScript patterns are ordinary arrays and objects with different behavior.

What an enum represents in JavaScript

An enum gives meaningful names to a fixed set of related values. In JavaScript, neither an array nor an object automatically has enum rules: each is a regular mutable value. Choose a representation based on how your code needs to access the values. An array is convenient when you need ordering or want to find a value’s position. An object is convenient when you already have a name and want its associated number.

Use an array when you need ordered values and lookup

An array preserves the order in which values are written. Its indexOf method returns the first matching position, or -1 when no match is found. It compares using strict equality, so values of different types do not match. If the same value occurs more than once, the first occurrence is returned.

The optional fromIndex argument sets where the search starts. A nonnegative value starts at that position; a negative value counts back from the end of the array. If the resulting start is beyond the last element, the search finds nothing. A negative start that falls before the beginning starts at the first element. For full details, see the indexOf reference.

Run this example:

const operatingSystems = ['Linux', 'macOS', 'Windows', 'Ubuntu'];
console.log(operatingSystems.indexOf('Linux'));

The returned number is the zero-based position of the first matching value. When handling a lookup, check for -1 before treating the result as a valid position. An array is also mutable: .push() can append a value, while .map() creates a new array from transformed values. Those operations do not make the array an enum or enforce a fixed set.

Build an enum-like object with a loop

If callers need to look up a number by name, a loop can create an object whose property names come from an argument list and whose values are their positions. This function uses arguments, the function-local collection of arguments passed to a traditional function. Enum is a user-defined function name here, not a JavaScript keyword or built-in type.

function Enum() {
  const values = {};
  for (let i = 0; i < arguments.length; i++) {
    values[arguments[i]] = i;
  }
  return values;
}

const operatingSystem = Enum('Linux', 'macOS', 'Windows', 'Ubuntu');
console.log(operatingSystem.Linux);

The property Linux is associated with 0, because it is the first argument. This version returns a plain object, so it can be used directly without new. If an argument name is repeated, a later assignment replaces the earlier property value. Since this object inherits from Object.prototype, this simple pattern is best for a controlled list of ordinary names; use a null-prototype object or a Map if names can come from untrusted or arbitrary input. The mapping is one-way: it does not add a reverse lookup from a number to its name, nor does it validate values assigned elsewhere in the program.

Choose a pattern and understand its limits

Use an array when order is important or you need to search values with indexOf. Use a name-to-number object when your input is a known name and direct property access is clearer than searching an array. In either case, JavaScript does not enforce that the collection stays fixed, prevent unknown values, or guarantee that the numeric positions remain stable if you reorder the entries.

For a small constant set, keep the values together and avoid changing them after initialization by convention. If code depends on persisted numeric values, network formats, or values shared across programs, assign explicit numbers and treat them as part of that interface; deriving numbers from array positions can silently change their meaning after a reorder. TypeScript enums can provide compile-time enum syntax, but they are a TypeScript feature and have their own emitted JavaScript behavior. Choose them when the TypeScript type system is part of your project, rather than assuming native JavaScript arrays or objects provide the same checks.