# to-valid-identifier

> Convert a string to a valid JavaScript identifier

Latest version **1.0.0** (published 2025-10-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install to-valid-identifier
pnpm add to-valid-identifier
yarn add to-valid-identifier
bun add to-valid-identifier
```

## Health

**Score 60/100 (C)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2025-10-15 |
| First published | 2024-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 2 |
| Unpacked size | 4.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 42 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | valid, identifier, safe, sanitize, variable, property, ecmascript, javascript, js, reserved, keyword, word, property |

## Links

- npm: https://www.npmjs.com/package/to-valid-identifier
- Repository: https://github.com/sindresorhus/to-valid-identifier
- Homepage: https://github.com/sindresorhus/to-valid-identifier#readme
- Issues: https://github.com/sindresorhus/to-valid-identifier/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/to-valid-identifier

## Dependencies (2)

- [@sindresorhus/base62](https://npm.io/package/@sindresorhus/base62.md) ^1.0.0
- [reserved-identifiers](https://npm.io/package/reserved-identifiers.md) ^1.0.0

## 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

- 1.0.0 (latest) — 2025-10-15
- 0.1.1 — 2024-05-28
- 0.1.0 — 2024-05-05

## README

# to-valid-identifier

> Convert a string to a valid [JavaScript identifier](https://developer.mozilla.org/docs/Glossary/Identifier)

## Install

```sh
npm install to-valid-identifier
```

## Usage

```js
import toValidIdentifier from 'to-valid-identifier';

toValidIdentifier('foo');
//=> 'foo'

toValidIdentifier('foo-bar');
//=> 'foo$j$bar'

toValidIdentifier('$');
//=> '$a$'

toValidIdentifier('undefined');
//=> '$_undefined$'
```

## API

### toValidIdentifier(string)

Convert the given string to a valid JavaScript identifier.

Different inputs will always generate unique identifiers.

## Use cases

- **Code Generation:** Automate safe variable naming in scripts, avoiding syntax errors from invalid characters.
- **Compilers/Transpilers:** Essential for non-JavaScript languages compiling to JavaScript, ensuring that identifiers are compliant.
- **Dynamic Function Names:** Generate unique and valid function names from dynamic content such as user inputs or database fields.
- **API Wrappers:** Convert API response properties into valid JavaScript object keys for easier access.
- **Template Processing:** Ensure template placeholders are converted to valid JavaScript identifiers when replaced with dynamic values.

## Related

- [is-identifier](https://github.com/sindresorhus/is-identifier) - Check if a string is a valid JavaScript identifier

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