> For the complete documentation index, see [llms.txt](https://docs.aurelia.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aurelia.io/templates/overview/globals.md).

# Globals

Learn how Aurelia 2 handles global variables in templates, the built-in list of accessible globals, and when to use them effectively.

Aurelia's standard template expression parser recognizes a **fixed built-in list** of JavaScript globals, including `Math`, `JSON`, and `Array`. There is no configuration option to add names to this list.

***

## Why Limit Global Access?

The built-in names let templates use common JavaScript utilities directly. Other names, including `window` and `document`, are resolved through the template's binding scope. Expose the values or operations your template needs through its view model, as shown below. Adding a property to `globalThis` does not add it to the parser's built-in list.

The standard parser recognizes these global names:

* Infinity
* NaN
* isFinite
* isNaN
* parseFloat
* parseInt
* decodeURI
* decodeURIComponent
* encodeURI
* encodeURIComponent
* Array
* BigInt
* Boolean
* Date
* Map
* Number
* Object
* RegExp
* Set
* String
* JSON
* Math
* Intl

## Using Other APIs and Helpers

For an API outside the built-in list, expose a view-model property or method. For example, `URL` is not a built-in template global, but a component can make it available:

```typescript
export class MyApp {
  readonly URL = URL;
  address = 'https://example.com/products?page=2';
}
```

```html
<p>Host: ${new URL(address).hostname}</p>
```

Here, `URL` is a property of `MyApp`, and the template displays `Host: example.com`. The same approach works for an imported helper function: expose it on the view model and call it from the template.

***

## Example Usages of Built-In Globals

Below are illustrative examples showing how to use these built-in globals in Aurelia templates. The syntax is identical to standard JavaScript, but you simply call them within Aurelia’s binding expressions.

### 1. Working with `JSON`

Serialize an object for debugging or quick display:

```html
<pre>${JSON.stringify(user, null, 2)}</pre>
```

### 2. Mathematical Operations with Math

Perform simple or complex calculations:

```html
<p>The square root of 16 is: ${Math.sqrt(16)}</p>
```

### 3. Conditional Rendering with isNaN

Use global numeric checks to conditionally display elements:

```html
<input type="text" value.bind="value" />
<p if.bind="isNaN(value)">This is not a valid number!</p>
```

### 4. Regular Expressions with RegExp

Construct inline regular expressions for quick validation:

```html
<input value.bind="email" placeholder="Enter email" />
<p if.bind="new RegExp('^\\S+@\\S+\\.\\S+$').test(email)">
  Valid Email Address
</p>
```

### 5. Dynamic Property Access with Object

Use Object methods for reflection or retrieval:

```html
<p>Property Value: ${Object.getOwnPropertyDescriptor(user, selectedProp)?.value}</p>
```

### 6. Set Operations with Set

De-duplicate arrays or combine sets inline:

```html
<p>Unique Values: ${[...new Set(numbersArray)]}</p>
```

### 7. Encoding & Decoding URLs

Leverage encodeURI / decodeURI for safe link construction:

```html
<a href.bind="encodeURI(externalLink)">Visit External Site</a>
<p>Original URL: ${decodeURI(externalLink)}</p>
```

### 8. Number Formatting with Intl.NumberFormat

Localize numbers, currency, or dates easily:

```html
<p>Price: ${new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(price)}</p>
```

### 9. Complex Array Manipulations

Filter, map, and transform arrays:

```html
<p>Active Items: ${Array.from(dataSet).filter(i => i.active).map(i => i.name).join(', ')}</p>
```

***

## Best Practices and Considerations

1. Use Sparingly
   * Keep business logic in your view models, not in templates. Inline calls to complex global functions (e.g., JSON.stringify on large data) can degrade performance and reduce readability.
2. Security
   * Treat templates as trusted application code. The built-in list controls name lookup; it does not make template expressions a security sandbox.
3. Performance
   * Bindings can reevaluate expressions when their observed dependencies change. If you repeatedly perform expensive operations (like JSON.stringify on large objects), consider handling them in the view model and binding to a computed property instead.
4. Reactivity
   * Accessing global objects doesn’t magically become reactive. If you want to update the UI when data changes, store and manipulate it in the view model, ensuring Aurelia’s change detection can pick it up.
5. Clarity and Testing
   * Test heavy logic in a view model or service, not in templates. This approach keeps your code testable with unit tests and fosters a separation of concerns.

By sticking to these guidelines, you can leverage Aurelia’s built-in global access without sacrificing maintainability or performance.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.aurelia.io/templates/overview/globals.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
