About the JavaScript Escape
JavaScript string literals reserve the backslash, so any backslash in your data has to be doubled before the engine will read it literally. The same goes for whichever quote character surrounds the literal. Pick the literal style at the top and only the quote you actually use gets a backslash, which keeps the result readable: a double-quoted literal leaves apostrophes alone, and template-literal mode escapes backticks and the ${ opener that would otherwise start an interpolation.
Line feeds become \n, carriage returns \r, tabs \t, and the rarer control codes come out as \xNN. Two characters get special treatment because they cause bugs that are painful to find: U+2028 and U+2029 are legal inside a JSON string but historically terminated a line of JavaScript source, so they are always written as \u2028 and \u2029.
Accented letters and emoji stay as themselves unless you tick the non-ASCII box, since source files are UTF-8 today. Tick it when the file has to survive a build pipeline or a terminal that mangles bytes above ASCII. For an escaped string that belongs in a JSON document instead of a source file, JSON Escape follows the narrower JSON rules, and JavaScript Unescape reverses whatever this tool produces.
How to use
- Paste the raw text into the left pane.
- Pick the literal style that matches the quotes in your code.
- Tick Add surrounding quotes if you want a complete literal rather than just the contents.
- Copy the escaped text and paste it into your source file.
Common questions
- Why is my apostrophe not escaped?
- Because the default style is a double-quoted literal, where an apostrophe is an ordinary character. Switch the style to single quotes or to "Escape both quote types".
- What happens to a dollar sign in template literal mode?
- A lone dollar sign is left alone. Only the two-character sequence ${ is escaped, because that is what starts an interpolation.
- Does it escape a forward slash?
- No. A forward slash needs no escape in a string. Escaping it only matters when the string sits inside an HTML script tag.
- Can I paste code, not just plain text?
- Yes. Multi-line source code is handled the same way; newlines become \n so the whole block collapses into one literal.