[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/flow](../../README) / [index](../README) / jsLiteral

# Function: jsLiteral()

> **jsLiteral**(`value`): `string`

Defined in: [flow/src/utils.ts:44](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/flow/src/utils.ts#L44)

Encode a value as a JavaScript string literal for embedding in an Alpine expression.

An `x-on:click` / `x-show` / `:class` attribute is *executable code*, not display text, so
HTML escaping is the wrong tool: `escapeAttr` lets an expression through intact and the
browser hands Alpine exactly what was written. Interpolating a value into `'…'` by hand
therefore turns any user-controlled string — a workspace slug, a tab name — into stored
XSS against every viewer. `JSON.stringify` produces a literal with quotes, backslashes and
line terminators already escaped; the surrounding attribute escaping then handles the HTML
layer, and Alpine sees the original value.

## Parameters

### value

`unknown`

The value to embed.

## Returns

`string`

A JavaScript string literal, quotes included.

## Example

```ts
"x-on:click": `tab = ${jsLiteral(name)}`   // tab = "my-tab"
```
