# User class The `User` class represents a user. An instance representing the current user is returned calling the [`user`](https://www.krenalis.com/docs/integrations/javascript-sdk/methods.md#user) method of `Krenalis`. For example: ```javascript const userId = krenalis.user().id(); ``` ## id The `id` method is used to get and set the identifier of the user. It always returns the user's identifier, or `null` if there is no identifier. To set the user's identifier, call the `id` method with an argument: - to remove the identifier, pass a `null` argument. - to change the identifier and resets the Anonymous ID, pass a non-empty `String` or a `Number` (the number will be converted to a `String`). If the passed identifier is the same of the current identifier, it does nothing. #### Syntax ```javascript id(id) ``` ```typescript id(id?: string | null): string | null ``` #### Parameters | Name | Type | Required | Description | |------|--------------------------------|----------|----------------------------------------------------------------------------| | `id` | `String` or `Number` | | User identifier to set. If it is `null`, the user's identifier is removed. | #### Examples ```javascript const userId = krenalis.user().id(); ``` ```javascript krenalis.user().id(null); ``` ```javascript krenalis.user().id('509284521'); ``` ## anonymousId The `anonymousId` method is used to retrieve and modify the Anonymous ID. It consistently returns the Anonymous ID. To modify the Anonymous ID, call the `anonymous` method with an argument: - to reset the Anonymous ID with a newly generated value, pass `null`. - to set the Anonymous ID with a specified value, pass a non-empty `String` or a `Number` (the number will be converted to a `String`). #### Syntax ```javascript anonymousId(id) ``` ```typescript anonymousId(id?: string | null): string ``` #### Parameters | Name | Type | Required | Description | |------|--------------------------------|----------|----------------------| | `id` | `String` or `Number` | | Anonymous ID to set. | #### Examples ```javascript const anonymousId = krenalis.user().anonymousId(); ``` ```javascript krenalis.user().anonymousId(null); ``` ```javascript krenalis.user().anonymousId('e2984831-431d-44ad-b1ec-4b901392fb67'); ``` ## traits The `traits` method is used to access and modify a user's traits. These traits are for the anonymous user if the user is anonymous, and for the non-anonymous user if non-anonymous. It consistently returns the user's traits. To modify the user's traits, call the `traits` method with an argument: - To remove all traits, pass a `null` argument. - To update the traits, provide a non-null `Object`. Since traits are serialized with `JSON.stringify`, they must consist only of serializable values and should not contain cyclic references. In case of serialization errors, a warning will be logged in the console. The provided traits will completely replace the current traits of the user, if any. #### Syntax ```javascript traits(traits) ``` ```typescript traits(traits?: Record | null): Record ``` #### Parameters | Name | Type | Required | Description | |----------|----------|----------|----------------------------------------------------| | `traits` | `Object` | | User's traits to set. `null` to remove all traits. | #### Examples ```javascript const traits = krenalis.user().traits(); ``` ```javascript krenalis.user().traits(null); ``` ```javascript krenalis.user().traits({ firstName: 'Emily', lastName: 'Johnson' }); ```