# JavaScript

Source: https://docs.interop.io/desktop/capabilities/app-preferences/javascript/index.html

## Overview

The App Preferences API is accessible via the [`io.prefs`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md) object.

> ℹ️ *See the JavaScript [App Preferences example](https://github.com/InteropIO/js-examples/tree/master/app-preferences) on GitHub.*

## Get

To retrieve the stored app preferences for the current app, use the [`get()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-get) method:

```javascript
const prefs = await io.prefs.get();
```

This method resolves with an [`AppPreferences`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/apppreferences/index.md) object with the following properties:

| Property | Type | Description |
|----------|------|-------------|
| `app` | `string` | The name of the app with which the stored preferences are associated. |
| `data` | `object` | The stored app preferences. |
| `lastUpdate` | `string` | Timestamp of the last update of the app preferences. |

To retrieve the stored app preferences for a specific app, pass an app name to the [`get()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-get) method:

```javascript
const appName = "clientlist";
const prefs = await io.prefs.get(appName));
```

To retrieve the stored app preferences for all apps of the current user, use the [`getAll()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-get) method:

```javascript
const prefs = await io.prefs.getAll();

// The returned object has an `all` property which contains a list of `AppPreferences` objects.
prefs.all.forEach(console.log);
```

## Set

To set the preferences for the current app, use the [`set()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-set) method:

```javascript
const prefs = { fontSize: 18 };

await io.prefs.set(prefs);
```

The `set()` method replaces entirely the stored app preferences. All existing properties of the `AppPreferences` object associated with the app will be removed and replaced with the ones of the argument provided to the `set()` method.

To set the preferences for a specific app, pass an options object as a second argument to the `set()` method and specify the name of the app:

```javascript
const prefs = { fontSize: 18 };
const options = { app: "clientlist" };

await io.prefs.set(prefs, options);
```

## Update

To update the preferences for the current app, use the [`update()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-update) method:

```javascript
const prefs = { fontSize: 18 };

await io.prefs.update(prefs);
```

The `update()` method modifies only the properties provided in the update object. The values of any already existing top-level properties will be overwritten. All other existing properties of the `AppPreferences` object associated with the app will remain intact.

To update the preferences for a specific app, pass an options object as a second argument to the [`update()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-update) method and specify the name of the app:

```javascript
const prefs = { fontSize: 18 };
const options = { app: "clientlist" };

await io.prefs.update(prefs, options);
```

## Clear

To remove the stored app preferences for the current app, use the [`clear()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-clear) method:

```javascript
await io.prefs.clear();
```

To remove the stored app preferences for a specific app, pass an app name to the [`clear()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-clear) method:

```javascript
const appName = "clientlist";

await io.prefs.clear(appName);
```

To remove the stored app preferences for all apps of the current user, use the [`clearAll()`](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md#API-clearAll) method:

```javascript
await io.prefs.clearAll();
```

## API Reference

For a complete list of the available App Preferences API methods and properties, see the [App Preferences API reference documentation](https://docs.interop.io/desktop/reference/javascript/app%20preferences/api/index.md).
