js-string-escape is a utility package designed to safely escape arbitrary JavaScript strings for inclusion as string literals within generated JavaScript code. Currently at version 1.0.1, the package appears to be stable but has not seen updates in approximately nine years (as of April 2026), indicating an abandoned or long-term unmaintained status. It adheres to ECMAScript 5.1 specifications for string escaping and has been tested across all Unicode code points, ensuring robust handling of diverse character sets. A key differentiation point is its explicit focus on JavaScript string literal safety, in contrast to JSON escaping rules; specifically, it will escape `\'` which is invalid in JSON, and does not restrict control characters as JSON does. This makes it suitable for code generation scenarios where the output needs to be valid JavaScript, but unsuitable for generating JSON.
npm install js-string-escapeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `js-string-escape` to safely prepare strings for insertion into JavaScript string literals, including special characters and non-string inputs, and verifies correctness using `eval`.
For JSON escaping, use `JSON.stringify()` or a dedicated JSON escaping library.
Use `const jsStringEscape = require('js-string-escape');` for all imports, ensuring your environment supports CommonJS or you are transpiling correctly.Explicitly cast inputs to string using `String(value)` or `value.toString()` if type safety is critical or if handling complex objects.
Consider evaluating alternative, actively maintained string escaping libraries if long-term support or security updates are critical for your project.
Ensure your file is a CommonJS module (`.js` without `"type": "module"` in `package.json`, or explicitly `.cjs`), or use `import * as pkg from 'pkg'` if the package provides an ESM export (which `js-string-escape` does not). For `js-string-escape`, you might need to use a bundler or stick to CJS.
Use the correct CommonJS `require` syntax: `const jsStringEscape = require('js-string-escape');`.No dependency data recorded yet.