---
title: PREF
project: jComponent V20
version: 20.015
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/reference/pref/
updated: 2026-11-18T13:58:14.460Z
---

# PREF

> Persistent user preferences.

`PREF` keeps user preferences, such as a theme or a language, between visits. The values are saved in the persistent cache described in [CACHE()](../globals/). The stored values are available from the `ready` [event](../events/) onward.

## API

### PREF.get(key)

Type: `method`

Reads a preference.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The name of the preference. |

Returns: `Object` The value.

Example: Read a preference
```js
ON('ready', function() {
	PREF.set('theme', 'dark');
	console.log(PREF.get('theme')); // dark
});
```

### PREF.set(key, value)

Type: `method`

Writes a preference and saves all preferences for one year.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | yes |  | The name of the preference. |
| `value` | `Object` | yes |  | The value. |

Returns: `Object` The written value.

Write preferences from the `ready` [event](../events/) onward, when the stored values are loaded. The key `set` is reserved and is ignored.

Example: Write a preference
```js
ON('ready', function() {
	console.log(PREF.set('language', 'en')); // en
	console.log(PREF.language); // en
});
```

### PREF.{key}

Type: `property`

Each stored preference is also a property of the object.

Returns: `Object` The value of the preference.

Stored values are restored when the library starts. Reading a property is equal to calling [PREF.get()](../pref/). To save a change, use [PREF.set()](../pref/).

Example: Restore a stored value
```js
DEF.onstorageread = function(callback) {
	var stored = {};
	stored['ui' + HASH('PREF').toString(36)] = { value: { theme: 'dark' }, expire: new Date(Date.now() + 60000) };
	callback(stored);
};

ON('ready', function() {
	console.log(PREF.theme); // dark
});
```
