# quote-js-string

> Escape a string and wrap it in quotes to produce a safe JavaScript string literal

Latest version **0.1.0** (published 2026-07-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install quote-js-string
pnpm add quote-js-string
yarn add quote-js-string
bun add quote-js-string
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2026-07-02 |
| First published | 2026-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22 |
| Dependencies | 0 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | escape, quote, string, literal, string-literal, javascript, js, source, codegen, sanitize, safe |

## Links

- npm: https://www.npmjs.com/package/quote-js-string
- Repository: https://github.com/sindresorhus/quote-js-string
- Homepage: https://github.com/sindresorhus/quote-js-string#readme
- Issues: https://github.com/sindresorhus/quote-js-string/issues
- Funding: https://github.com/sindresorhus/quote-js-string?sponsor=1
- npm.io page: https://npm.io/package/quote-js-string

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.1.0 (latest) — 2026-07-02

## README

# quote-js-string

> Escape a string and wrap it in quotes to produce a safe JavaScript string literal

Useful for code generators, ESLint autofixers, and other tooling that needs to turn an arbitrary runtime string into JavaScript source code.

Escapes backslashes, the quote character, and unsafe characters (C0 control characters, DEL, the U+2028/U+2029 line terminators, which can disrupt line-oriented tooling, and lone surrogates, which have no valid UTF-8 representation).

## Install

```sh
npm install quote-js-string
```

## Usage

```js
import quoteJsString from 'quote-js-string';

quoteJsString('It\'s a test');
//=> `'It\\'s a test'`

quoteJsString('It\'s a test', '"');
//=> `"It's a test"`

quoteJsString('line1\nline2');
//=> `'line1\\nline2'`
```

## API

### quoteJsString(string, quote?)

#### string

Type: `string`

The string to quote and escape.

#### quote

Type: `'` | `"`\
Default: `'`

The quote character to wrap the string in.

---
_Source: https://npm.io/package/quote-js-string · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
