JavaScript String Escaper / Unescaper
Paste text to get a string literal you can drop into code — single, double or backtick quoted, or as JSON. Or paste an escaped string with \n and \u sequences to read what it really says.
Runs entirely in your browser — nothing you enter here is sent to a server.
What has to be escaped in a JavaScript string
A string literal ends at the next unescaped quote of the same kind, so that quote has to be written as \" or \'. The backslash itself starts every escape sequence, so a real backslash is \\ — the reason Windows paths so often break in code. Line breaks cannot appear inside a quoted string at all and are written as \n; tabs and other control characters have their own short forms.
"…" and '…'
Identical in JavaScript except for which quote needs escaping. Pick the one that appears less in your text.
`…` template literals
May contain real line breaks and both quote kinds, but a backtick and the sequence ${ must be escaped, or the latter starts an expression.
JSON strings
Stricter: double quotes only, no \x, \v or \u{…} forms, and every control character must be escaped. The JSON option follows those rules.
\u escapes
\u00e9 is é. Characters beyond U+FFFF, like most emoji, need two of them (a surrogate pair) or the newer \u{1F680} form.
Strings inside HTML script tags
A string that ends up inside an inline <script> block has one more trap: the HTML parser ends the script at the first </script, even in the middle of a string. Writing <\/ instead is the same string to JavaScript but invisible to the HTML parser. The "safe inside HTML" option does that, and also breaks up <!--, which can change how the script block is parsed.
For data you put into a page from the server, the most robust approach is to serialise it as JSON with these characters escaped, or to put it in a data- attribute with HTML escaping and read it from the script.
Frequently asked questions
How do I escape a double quote in JavaScript?
Write a backslash before it: "She said \"hi\"". Or use single quotes or a template literal around the string, where a double quote needs no escaping.
How do I write a backslash in a JavaScript string?
Double it: "C:\\temp\\file.txt". A single backslash starts an escape sequence, so "C:\temp" contains a tab character instead of \t.
What is the difference between escaping for JavaScript and for JSON?
JSON allows only double-quoted strings and a smaller set of escapes: no \x, \v, \0 or \u{…}. A JSON-escaped string is valid JavaScript, but not every JavaScript string is valid JSON.
Is unescaping done with eval?
No. The escape sequences are decoded by a small parser in this page, so pasting code here never runs it.