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

# Structures

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

## Struct Syntax

A structure groups one or more named fields, of any type, into a new type. For example:

```slice
module M
{
    struct TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }
}
```

This definition introduces a new type named `TimeOfDay`. The name of a field must be unique within its structure.

A structure contains only fields: as elsewhere in Slice, type definitions cannot be nested, except for [modules](https://docs.zeroc.com/ice/3.8/slice/modules/js.md). Like every other type, you define a structure in a module.

## Language Mapping

A Slice structure maps to a JavaScript class with the same name. For each Slice field, the JavaScript instance contains a corresponding field. As an example, here is our Employee structure once more:

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

The mapping for this structure is equivalent to the following JavaScript code:

```js
// Generated JavaScript code

class Employee {
    constructor(number = 0n, firstName = "", lastName = "") {
        this.number = number;
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
```

```typescript
// Generated TypeScript definition

class Employee {
    constructor(number?: bigint, firstName?: string, lastName?: string);
    clone():Employee;
    equals(other: any): boolean;
    hashCode(): number;

    number:bigint;
    firstName:string;
    lastName:string;
}
```

The generated class defines an `equals` method for comparison purposes and a `clone` method that returns a copy of the structure: `clone` calls the `clone` method of each field value that has one, such as a structure, copies each `Array` field into a new array that holds the same elements, and assigns every other field as is. For structures that are also [legal dictionary key types](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/js.md), the mapped class also defines a `hashCode` function as required by the `Ice.HashMap` type.

### Generated Constructor

The generated constructor has one parameter for each field. This allows you to construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its fields).

All these parameters have also default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/js.md)).

## See Also

- [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/js.md)
- [Classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/js.md)
