JavaScript Escape / Unescape
Escapes or unescapes a JavaScript string, replacing characters that could prevent interpretation with their escape sequences.
Result
JavaScript string escaping explained
Why escape?
JavaScript string literals can be delimited by single quotes, double quotes or backticks. Whichever delimiter you pick, a literal occurrence of that delimiter, a backslash or a raw line break inside the value breaks the literal. Escaping produces text that is safe to paste between either kind of quote, which is why this tool escapes both quote characters.
Rules applied by this tool
| Character | Escaped as | Notes |
|---|---|---|
| Horizontal tab (U+0009) | \t | |
| Vertical tab (U+000B) | \v | |
| Null character (U+0000) | \0 | |
| Backspace (U+0008) | \b | |
| Form feed (U+000C) | \f | |
| Newline (U+000A) | \n | |
| Carriage return (U+000D) | \r | |
| Single quote | \' | |
| Double quote | \" | |
| Backslash | \\ | Handled first. |
| Line separator / paragraph separator (U+2028, U+2029) | \u2028, \u2029 | Historically illegal inside string literals; still escaped for safety. |
| Other control characters | \x00 … \x1F, \x7F | |
| Non-ASCII characters | \u00E9, surrogate pairs for emoji | Only with the "\uXXXX" option. |
Before / after
| Input | Escaped output |
|---|---|
It's a "test" | It\'s a \"test\" |
C:\temp\new | C:\\temp\\new |
| Two lines separated by a line break | first\nsecond |
Café 😀 (with the \uXXXX option) | Caf\u00E9 \uD83D\uDE00 |
Unescaping
The unescape button decodes every sequence above plus \xXX, \uXXXX, \u{X…} (ES2015 code point escapes), legacy octal escapes (\101) and line continuations (a backslash immediately followed by a line break, which produces nothing). An unknown escape such as \q simply yields q, which is what JavaScript itself does.
Escaping is not sanitizing
Escaping quotes makes a value safe inside a string literal; it does not make it safe to inject into HTML or into eval(). To embed data in a page, serialize it with JSON.stringify() and additionally replace < with \u003c so that a value containing </script> cannot close your script tag. In modern code, template literals and String.raw reduce the need for manual escaping considerably.
const s = 'It\'s a "test"'; // escaped literal
const safe = JSON.stringify(userInput).replace(/</g, '\\u003c');
const path = String.raw`C:\temp\new`; // no escaping needed