---
title: "Enumerations"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations
language: js
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Enumerations

Programming language: JavaScript. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/java.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/swift.md).

## Enumeration Syntax and Semantics

An enumeration defines a set of named values, its enumerators:

```slice
module M
{
    enum Fruit { Apple, Pear, Orange }
}
```

This definition introduces a new type named `Fruit`. By default, the first enumerator has a value of zero, with sequentially increasing values for subsequent enumerators.

A Slice enum type introduces a new namespace scope, so the following is legal:

```slice
module M
{
    enum Fruit { Apple, Pear, Orange }
    enum ComputerBrands { Apple, Dell, HP, Lenovo }
}
```

The example below shows how to refer to an enumerator from a different scope:

```slice
module M
{
    enum Color { Red, Green, Blue }
}

module N
{
    struct Pixel
    {
        M::Color c = Blue;
    }
}
```

Slice does not permit empty enumerations.

> **Note**
>
> In Ice releases prior to Ice 3.7, an enum type did not create a new namespace and its enumerators were in the same namespace as the enum type itself. With these releases, you had to select longer enumerator names to avoid a naming clash.

## Custom Enumerator Values

Slice also permits you to assign custom values to enumerators:

```slice
const int PearValue = 7;
enum Fruit { Apple = 0, Pear = PearValue, Orange }
```

Custom values must be unique and non-negative, and may refer to Slice constants of integer types. If no custom value is specified for an enumerator, its value is one greater than the enumerator that immediately precedes it. In the example above, `Orange` has the value 8.

The maximum value for an enumerator value is the same as the maximum value for `int`, 2<sup>31</sup> - 1.

Slice does not require custom enumerator values to be declared in increasing order:

```slice
enum Fruit { Apple = 5, Pear = 3, Orange = 1 }   // Legal
```

The language mapping determines whether a program can compare two enumerators for order, and whether such a comparison follows the enumerators' values or their declaration order.

> **Warning**
>
> For an application that is still using version 1.0 of the [Ice encoding](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md), changing the definition of an enumerated type **may** break backward compatibility with existing applications. For more information, please refer to the [encoding rules](https://docs.zeroc.com/ice/3.8/encoding/basic-data-encoding.md) for enumerators.

## Language Mapping

JavaScript does not have an enumerated type, so a Slice enumeration is emulated using JavaScript objects where each enumerator is an instance of the same type. For example:

```slice
enum Fruit { Apple, Pear, Orange }
```

The generated code is equivalent to the following JavaScript code:

```js
class Fruit { ... }
Fruit.Apple = new Fruit("Apple", 0);
Fruit.Pear = new Fruit("Pear", 1);
Fruit.Orange = new Fruit("Orange", 2);
```

The corresponding TypeScript declarations are:

```typescript
export class Fruit extends Ice.EnumBase {
    static readonly Apple: Fruit;
    static readonly Pear: Fruit;
    static readonly Orange: Fruit;

    static valueOf(value: number): Fruit | undefined;
}
```

Each enumerator has the read-only `name` and `value` properties of `Ice.EnumBase`, which supply the enumerator's name and value, and a `toString` method that returns its name. The static `valueOf` method returns the enumerator with a given value.

Suppose we modify the Slice definition to include a custom enumerator value:

```slice
enum Fruit { Apple, Pear = 3, Orange }
```

The generated code changes accordingly:

```js
class Fruit { ... }
Fruit.Apple = new Fruit("Apple", 0);
Fruit.Pear = new Fruit("Pear", 3);
Fruit.Orange = new Fruit("Orange", 4);
```

Given the above definitions, we can use enumerated values as follows:

```js
const f1 = Fruit.Apple;
const f2 = Fruit.Orange;

if (f1 === Fruit.Apple) { // Compare with constant
    // ...
}

if (f1 === f2) {         // Compare two enums
    // ...
}

switch(f2) {             // Switch on enum
    case Fruit.Apple:
        // ...
        break;
    case Fruit.Pear:
        // ...
        break;
    case Fruit.Orange:
        // ...
        break;
}

// Convert a value to its enumerator, or undefined if no match
const f = Fruit.valueOf(3);
console.log(f.name + " = " + f.value); // Outputs "Pear = 3"
```

> **Note**
>
> The TypeScript declarations describe the same generated code: `Fruit` is a class, not a TypeScript `enum`, and `Fruit.Apple` is an instance of this class, not a number.

## See Also

- [Constants and Literals](https://docs.zeroc.com/ice/3.8/slice/constants-and-literals/js.md)
