JavaScript Escape / Unescape
Escape text into a JavaScript string literal, or unescape JavaScript escape sequences back to plain text.
Escaped output will appear here.What is JavaScript escaping?
It represents arbitrary text as a valid JavaScript string literal, so the text can sit safely between quote characters in source code without breaking the surrounding syntax or losing any characters.
Quotes, backslashes, and control characters
Three kinds of characters need attention. A literal single or double quote - whichever character delimits the string - is escaped so it doesn't end the literal early. A literal backslash is escaped so it isn't misread as the start of the next escape sequence. And control characters with no printable form, like newline, tab, carriage return, and backspace, are written as short backslash sequences since typing them directly into source code either isn't possible or would break formatting.
Supported escape sequences
\n- newline\t- tab\r- carriage return\b- backspace\f- form feed\v- vertical tab\0- null\xHH- byte escape\uHHHH- Unicode code unit\u{...}- code point
A practical example
Say a value needs to become a JavaScript string literal you can paste into code: a two-line piece of text that itself contains a double quote, such as a snippet reading She said quote hi quote then pressed enter. Escaped, the quote characters become a backslash-quote pair and the line break becomes the newline escape sequence, so the whole thing turns into one valid, single-line string literal that can be assigned to a variable or passed as an argument without touching the surrounding code.
Common escaping mistakes
- Escaping the wrong quote character - if the string will be wrapped in single quotes, only the single quote needs escaping inside it; escaping double quotes that don't delimit the string is unnecessary and can look wrong to a reviewer.
- Missing a backslash inside a file path - a Windows-style path copied in as-is has unescaped backslashes that JavaScript will try to interpret as the start of escape sequences, often producing a garbled or invalid string.
- Escaping text twice - running already-escaped text through an escaper again doubles every backslash, so unescaping it once no longer recovers the original text.
- Leaving raw line breaks inside a single-quoted or double-quoted literal - JavaScript string literals written with quotes, not backticks, can't contain a literal newline; it has to be the escape sequence instead.
JavaScript escaping vs. URL encoding, HTML escaping, and JSON escaping
These four are easy to mix up because they all turn "unsafe" characters into safe sequences, but each targets a different context. JavaScript escaping protects a string literal inside JS source code. URL encoding (percent-encoding) protects text placed inside a URL, replacing spaces, slashes, and other reserved or non-ASCII characters with percent-prefixed codes. HTML escaping protects text placed inside HTML markup, mainly by converting angle brackets and ampersands into entities so they aren't read as tags. JSON escaping is close to JavaScript escaping but follows JSON's own, slightly stricter grammar. Text prepared for one of these contexts is generally not safe to drop directly into another - a JavaScript-escaped string pasted into a URL, for instance, still needs its own URL encoding.
When to use this tool vs. escaping by hand
Escaping a short string by hand is reasonable when there are only one or two characters to change and you can see the whole string at a glance. It gets riskier with longer text, text pulled from a file or another system, or text with a mix of quotes, backslashes, and non-printable characters, where it's easy to miss a character and end up with broken or subtly incorrect output. This tool applies the same rules to every character consistently, which is worth the trip whenever the input isn't trivial.
Frequently asked questions
- What does JavaScript string escaping actually do?
- It rewrites arbitrary text as a valid JavaScript string literal by converting characters that would otherwise break the literal - quotes and backslashes - or that have no literal representation, like newlines and tabs, into backslash-prefixed escape sequences the JavaScript engine understands.
- Does escaping change my text?
- No. Escaping only changes how the text is represented in source code, not what it says. Escaping and then unescaping returns exactly the original text, character for character.
- Why do quotes and backslashes need escaping in the first place?
- A JavaScript string literal is delimited by a matching pair of quote characters, so a literal quote inside the text would end the string early unless it's escaped. A literal backslash is escaped for a related reason: backslash is what signals the start of an escape sequence, so an unescaped one would be misread as the beginning of the next sequence instead of a plain character.
- How is JavaScript escaping different from URL encoding, HTML escaping, or JSON escaping?
- Each protects text for a different context. JavaScript escaping makes text safe to embed inside a JS string literal in source code. URL encoding makes text safe inside a URL by replacing reserved and non-ASCII characters with percent sequences. HTML escaping stops characters like the angle bracket from being read as markup. JSON escaping is similar to JavaScript escaping but follows the stricter JSON grammar, which has a smaller set of allowed sequences. Text destined for one context generally needs its own escaping, not another context's.
- When should I use this tool instead of escaping text by hand?
- Hand-escaping is fine for a short, simple string, but it gets error-prone fast with longer text, text you didn't write yourself, or text containing a mix of quotes, backslashes, and non-printable characters - it's easy to miss one and end up with a syntax error or subtly wrong string. This tool handles every character consistently and is worth using whenever the input isn't trivial or comes from an untrusted or unfamiliar source.