JavaScript string escaper
Text with quotes and a line break, such as Hello "world" over two lines, becomes Hello \"world\"\nline 2, ready to paste between double quotes. Choose double, single or backtick quotes, or unescape an escaped string back into plain text. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Text
- Hello "world" line 2
- Calculation mode
- Escape
- Quote
- Double
- Result
- Hello \"world\"\nline 2
- Length
- 23
- Literal
- "Hello \"world\"\nline 2"
Embedding free text inside a JavaScript string literal is a daily footgun: a single double quote ends the string, a backslash swallows the next character, a raw newline is a syntax error, and non-ASCII paste can break older tooling that expects \uXXXX. This JavaScript string escaper turns plain text into a safe string-literal body (and a ready-to-paste quoted literal), and reverses the same map when you need the original characters back. Escape mode handles the quote style you choose—double, single, or backtick—plus backslashes, the usual control escapes, and optional unicode escapes for every non-ASCII code point.
How JavaScript string escapes work in practice
A JavaScript string literal is a sequence of characters wrapped in double quotes, single quotes, or backticks. Inside that sequence, a backslash starts an escape: \\ becomes one backslash, \" or \' matches the chosen quote, \n is a line feed, \r a carriage return, \t a tab, and short forms such as \0, \b, \f, and \v cover other controls. Two hex forms are standard in modern engines: \xHH encodes a single byte-like code unit with two hexadecimal digits, and \uXXXX encodes a UTF-16 code unit with four digits.
ES6 also allows \u{1F600} style braces for any Unicode code point up to U+10FFFF, which is the clean way to represent characters outside the Basic Multilingual Plane without hand-rolled surrogate pairs. This capability applies those rules deterministically. In escape mode it walks each code unit, replaces special characters with the shortest safe escape for the selected quote style, always escapes U+2028 and U+2029 so a multi-line paste cannot break source lines, and optionally rewrites every non-ASCII character to \uXXXX or \u{…} when unicode is true.
In unescape mode it parses the same sequences back to characters and rejects trailing backslashes, incomplete \x or \u forms, empty \u{}, out-of-range code points, and legacy octal escapes that only exist in sloppy Annex B modes.
Where a JS string escaper earns its keep
Documentation and cookbook sites that show copy-paste examples need a reliable escape path so user-supplied samples remain valid JavaScript. Support and security teams paste log fragments that already contain backslash escapes and need an unescape path that fails loudly on truncated \u sequences instead of inventing characters. Localization engineers verify that multi-byte languages round-trip when unicode mode is forced for ASCII-only repositories.
Curriculum authors teaching ECMAScript string syntax want one dedicated page for string-literal escapes rather than burying the topic under a generic Base64 or HTML-entity tool, because search intent and the allowed escape set differ. If you need HTML entities, URL percent-encoding, or full JavaScript parsing, treat those as separate search intents and landings rather than overloading this string escaper page.
What you can do with it
Safe string literals in generated code
Escape free-text titles, messages, and config values before writing them into double-quoted, single-quoted, or template-literal JavaScript source.
ASCII-only unicode rewriting
Escape with unicode true so non-ASCII characters become \uXXXX or \u{…} sequences suitable for ASCII-only repositories and legacy toolchains.
FAQ
What is the difference between result and literal?
Result is the escaped body without surrounding quotes. Literal (escape mode only) wraps that body in the chosen quote style so you can paste a complete JavaScript string into source.
Can I both escape and unescape?
Yes. Set mode to escape (default) to encode plain text, or mode to unescape to recover text from a JavaScript string-literal body.
Which quote styles are supported?
Double (default), single, and backtick. Escape mode only escapes the delimiter you chose; backtick mode also escapes ${ so the body is safe inside a template literal.
What does it cost?
The browser tool is free and runs locally.
How do I turn accented letters into \u escapes?
Switch on the Unicode option when escaping. Every non-ASCII character is then rewritten, so café becomes caf\u00E9, and characters beyond the basic range use the \u{…} form instead.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/enc/js-string-escape \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"text":"Hello \"world\"\nline 2"}'const res = await fetch("https://api.kit.forhosting.com/enc/js-string-escape", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"text": "Hello \"world\"\nline 2"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/enc/js-string-escape",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"text": "Hello \"world\"\nline 2"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/enc/js-string-escape", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"text":"Hello \\"world\\"\\nline 2"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"text":"Hello \"world\"\nline 2"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/enc/js-string-escape", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"text": "Hello \"world\"\nline 2"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "enc.js_string_escape",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Unescape mode understands \n, \r, \t, \\ , \xHH, \uXXXX, and \u{…}, and fails immediately with invalid_input on truncated or illegal sequences instead of inventing half-decoded garbage.
How JavaScript string escapes work in practice
The path is pure string work with no network, no random seeds, and no wall-clock timestamps, so identical inputs always produce identical JSON. Pricing for successful API calls is the published base of $0.002 per item, and validation failures do not consume prepaid balance.
What you send and what you get back
Send a required string field named text and optional mode, quote, and unicode fields. Mode escape converts plain text into a JavaScript string-literal body stored in result, and also returns literal: the same body wrapped in the chosen quotes so you can paste it into source. Mode unescape reverses escape sequences in text and returns the recovered plain string in result without a literal field. Quote defaults to double; set single or backtick when the destination syntax uses those delimiters—escape only forces an escape for that delimiter (and, for backticks, for the ${ sequence that would open an interpolation). Classic checks include escaping a greeting that contains embedded quotes and a newline, unescaping a body that uses \u00E9 for é, escaping with unicode true so café becomes caf\u00E9, and rejecting a string that ends with a bare backslash. When mode is omitted, escape is the default so a single payload still does useful work. The response echoes mode, quote, unicode, the original text, result, and the length of result. Missing text, non-string objects, unsupported mode or quote values, values that cannot be read as a boolean for unicode, strings longer than the documented maximum, trailing backslashes, incomplete hex escapes, non-hex digits, empty brace escapes, illegal code points, and rejected octal forms all fail with invalid_input and a clear English message. Validation failures do not consume prepaid balance. Successful API pricing is the published base of $0.002 per item.
Where a JS string escaper earns its keep
Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.
Integration use cases
Decode escaped log and fixture bodies
Unescape JavaScript string bodies from logs, tickets, and golden files back to readable text, with hard failures on incomplete or illegal escapes.
Technical questions
What happens if an escape sequence is malformed?
Unescape fails with invalid_input, naming the position and the bad sequence (for example a trailing backslash or incomplete \uXXXX). Nothing is charged for validation failures.