---
title: "Dictionaries"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries
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).

# Dictionaries

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

## Dictionary Syntax and Semantics

A dictionary is an associative array: a collection of key-value pairs in which each key is unique.

For example:

```slice
module M
{
    struct Employee
    {
        long   number;
        string firstName;
        string lastName;
    }

    dictionary<long, Employee> EmployeeMap;
}
```

This definition creates a dictionary named `EmployeeMap` that maps from an employee number to a structure containing the details for an employee.

## Allowable Types for Dictionary Keys and Values

The value type of a dictionary can be any Slice type. However, the key type of a dictionary is limited to one of the following types:

- [Integral](https://docs.zeroc.com/ice/3.8/slice/basic-types/js.md) types (`short`, `int`, `long`)
- [bool](https://docs.zeroc.com/ice/3.8/slice/basic-types/js.md)
- [byte](https://docs.zeroc.com/ice/3.8/slice/basic-types/js.md)
- [string](https://docs.zeroc.com/ice/3.8/slice/basic-types/js.md)
- [enum](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/js.md)
- [Structures](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/js.md) containing only fields of legal key types

## Language Mapping

A Slice dictionary maps to:

- A JavaScript [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) when the key is a Slice basic type or an enumeration.
- An [`Ice.HashMap`](https://code.zeroc.com/ice/3.8/api/javascript/Ice/HashMap.html) when the key is a Slice structure.

This distinction is necessary because:

- A JavaScript `Map` uses the `===` operator for key equality.
- `Ice.HashMap` accepts a key comparator, so struct keys can use their `equals` method for equality.

### Example: Dictionary with a Basic Key Type

```slice
struct Employee
{
    long number;
    string firstName;
    string lastName;
}

dictionary<long, Employee> EmployeeMap;
```

In this example, `EmployeeMap` maps to a JavaScript `Map` with:

- key type = `BigInt` (from Slice `long`)
- value type = `Employee` (the JavaScript class generated from the Slice struct).

### Example: Dictionary with Struct Key

If the key is a Slice struct, the compiler generates code that uses [Ice.HashMap](https://code.zeroc.com/ice/3.8/api/javascript/Ice/HashMap.html).

```slice
dictionary<Employee, string> EmployeeDeptMap;
```

The generated TypeScript declaration is:

```typescript
class EmployeeDeptMap extends Ice.HashMap<Employee, string> {}
```

You create and use an `EmployeeDeptMap` like a `Map`:

```js
const departments = new EmployeeDeptMap();
departments.set(new Employee(42n, "Stan", "Lippman"), "Engineering");
departments.get(new Employee(42n, "Stan", "Lippman")); // "Engineering"
```
