---
title: Paths
project: jComponent V20
version: 20.015
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/guides/paths/
updated: 2026-11-18T13:55:34.124Z
---

# Paths

> How to address, read, change and watch values of the model with string paths.

A path is a string that addresses a value in the global `window` object: `app.user.name` is the same value as `window.app.user.name`. All data in jComponent is read and written through paths, which is how the library knows what has changed and who needs to be told.

## Reading and writing values

`SET(path, value)` writes a value and notifies everything that depends on the path. `GET(path)` reads a value.

```js
SET('app.user.name', 'Ana');

console.log(app.user.name);
console.log(GET('app.user.name'));
console.log(GET('app.user'));
console.log(GET('app.company.name'));
```

```text
Ana
Ana
{"name":"Ana"}
undefined
```

`SET()` creates the missing objects along the path. `GET()` returns `undefined` when a part of the path does not exist.

The first part of a path is a global variable, so choose a name that the browser does not already use. Names such as `name`, `location` or `status` belong to `window`.

## Path syntax

| Path | Meaning |
| --- | --- |
| `app.user.name` | A nested key |
| `shop.products[1].name` | A key of an existing array item |
| `shop.tags[]` | With `SET()`, appends the value to an array and creates the array when it is missing |
| `shop.prices['pen.blue']` | A key that contains a dot |

```js
SET('shop.products', [{ name: 'Pen' }, { name: 'Book' }]);
SET('shop.products[1].name', 'Notebook');
console.log(GET('shop.products[1].name'));

SET('shop.tags[]', 'new');
SET('shop.tags[]', 'sale');
console.log(GET('shop.tags'));

SET("shop.prices['pen.blue']", 12);
console.log(GET("shop.prices['pen.blue']"));
```

```text
Notebook
["new","sale"]
12
```

## Changing values

| Function | What it does |
| --- | --- |
| `SET(path, value)` | Writes a value |
| `INC(path, [value])` | Adds a number to the value, `1` by default |
| `TOGGLE(path)` | Negates the value |
| `PUSH(path, value)` | Appends a value to an existing array. With the prefix `^`, the value goes to the start of the array |
| `NUL(path)` | Writes `null` |

```js
SET('cart.count', 1);
INC('cart.count');
INC('cart.count', 5);
console.log(cart.count);

TOGGLE('cart.open');
console.log(cart.open);

SET('cart.items', ['b']);
PUSH('cart.items', 'c');
PUSH('^cart.items', 'a');
console.log(cart.items);

NUL('cart.count');
console.log(cart.count);
```

```text
7
true
["a","b","c"]
null
```

## Who is notified

A change reaches listeners on the same path, on its parent paths and on its child paths. Listeners on sibling paths are left alone.

```js
WATCH('order', () => console.log('order'));
WATCH('order.customer', () => console.log('order.customer'));
WATCH('order.customer.name', () => console.log('order.customer.name'));
WATCH('order.total', () => console.log('order.total'));

SET('order.customer', { name: 'Ana' });
```

```text
order
order.customer
order.customer.name
```

The watcher of `order.total` does not run, because `order.customer` is neither its parent nor its child.

## Changing the model directly

The model is a plain object, so you can change it with normal JavaScript. The library does not see such a change. Call `UPD(path)` to announce it:

```js
SET('todo.items', ['Write']);

WATCH('todo.items', function(value, flags, path) {
	console.log(path, value.length);
});

// Changing the object directly notifies nobody
todo.items.push('Test');

// UPD() tells everybody that the value has changed
UPD('todo.items');
```

```text
todo.items 2
```

## Watching a path

`WATCH(path, callback, [init])` runs a function whenever the value at the path changes. `UNWATCH(path, [callback])` removes the watcher. Without the `callback` argument, it removes all watchers of the path.

The callback receives three arguments:

| Argument | Description |
| --- | --- |
| `value` | The current value at the watched path |
| `flags` | An object with the [flags](../flags/) of the operation |
| `path` | The path that was changed, which can be a parent or a child of the watched path |

```js
function onchange(value, flags, path) {
	console.log(path, value);
}

WATCH('stats.visits', onchange);
SET('stats.visits', 1);

UNWATCH('stats.visits', onchange);
SET('stats.visits', 2);

// Several paths with one callback
WATCH('stats.a + stats.b', onchange);
SET('stats.a', 10);
SET('stats.b', 20);

// The third argument calls the callback right after the registration
SET('stats.c', 30);
WATCH('stats.c', onchange, true);
```

```text
stats.visits 1
stats.a 10
stats.b 20
stats.c 30
```

To display a value on the page, you do not need a watcher. Use [data binding](../data-binding/).

## Shortcuts

Paths support prefixes for common places in the model, such as `*title` for `common.title`. They are described in [Flags and path modifiers](../flags/).
