> For the complete documentation index, see [llms.txt](https://docs.zerowork.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zerowork.io/building-blocks/external-and-integrations/write-javascript.md).

# Write JavaScript

<figure><picture><source srcset="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-48212c10342bc35518ea44eafedbebe3e25df7ab%2Fwrite-javascript-tile-dark.png?alt=media" media="(prefers-color-scheme: dark)"><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-c9742ac48c391f1bd284d9fea459d1a451696bda%2Fwrite-javascript-tile.png?alt=media" alt="Write JavaScript block" width="279"></picture><figcaption></figcaption></figure>

**Write JavaScript** runs your own code. Use it for anything the other building blocks can't do: read or change the page, transform data, call a library, work with files.

<figure><picture><source srcset="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-05a1df431d2e57639756555f8c850828c83436e3%2Fwrite-javascript-settings-dark.png?alt=media" media="(prefers-color-scheme: dark)"><img src="https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2Fgit-blob-9a51b22546673d05c8ad6d5506ef5b00059a153d%2Fwrite-javascript-settings.png?alt=media" alt="Write JavaScript settings" width="1092"></picture><figcaption></figcaption></figure>

## Browser vs. local execution

The same building block can run your code in two places:

|                 | In the browser page (default)                                            | Locally in the Desktop Agent                                                           |
| --------------- | ------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- |
| How to select   | Default                                                                  | Check **Run locally in the app**, or put `// @zw-run-locally` anywhere in the code     |
| Where it runs   | Inside the page that is currently open, like code in the browser console | In Node.js on your device                                                              |
| Use it for      | Reading or changing the page with `document` and `window`                | npm packages, files and other system features, controlling the browser with Playwright |
| `console.log()` | Shows in the browser console. Can also use `zw.log()`                    | Not shown. Use `zw.log()`                                                              |

Browser execution:

```javascript
const prices = [...document.querySelectorAll(".price")].map((el) => el.textContent.trim());
await zw.log(prices.join(", "));
```

Local execution:

```javascript
// @zw-run-locally
const fs = require("fs");
const page = zw.browserContext.getActivePage();
fs.appendFileSync("/Users/me/titles.txt", (await page.title()) + "\n");
```

### zw API

Inside the code, the `zw` object connects your code to the TaskBot: read and write tables and variables, import npm packages, write logs, add delays, keep state, use device storage and control the browser. Some of it only works in local execution.

See the [zw.\* API](https://docs.zerowork.io/zw.-api/overview.md) section for all methods.

### Errors

An error thrown in your code, for example `throw new Error("Price not found")`, fails the building block like any other error: the run ends, unless the block is inside a [Try-Catch scope](https://docs.zerowork.io/building-blocks/logic/try-catch.md).

The return value of your code is not logged. Use `zw.log()` to see a value in the run logs.
