Format Code Now
/
Encode & decodeJavaScript String Escape Next: SQL String Escape ›

JavaScript Escape and Unescape

ED-06

Paste text to escape it for a JavaScript string with single quotes, double quotes or backticks, or paste an escaped string to read the plain text. It runs no code.

Runs in your browser. Nothing is uploaded.

Text or string literal content

Result

How to use it

  1. Paste your text into the input box.
  2. Pick the quote style your string uses: single, double or template literal (backticks).
  3. Choose Escape or Unescape. Tick Add surrounding quotes to get the full literal.
  4. Copy the result or download it as a .txt file.

What Escape changes

Escape adds a backslash before the backslash itself and before the quote you chose. Line breaks, tabs and other control characters become \n, \t, \xNN and so on. A NUL becomes \0, unless a digit follows it, when \x00 is used so the digit is not read as part of the escape. U+2028 and U+2029 are written as \u2028 and \u2029. In template mode, backticks and ${ are escaped. Optional boxes escape non-ASCII characters as \uXXXX and the < character as \x3C.

What Unescape understands

Unescape reads \n, \r, \t, \b, \f, \v, \0, \xNN, \uNNNN, \u{...} and line continuations. A backslash before any other character gives that character, as in JavaScript. Old octal escapes such as \012 are rejected with an error, because modern JavaScript does not allow them in strict mode or template literals. Matching outer quotes are removed if present. Nothing is evaluated, so the text is only parsed, never run.

Escaping is not a security fix

Putting a string safely into a JavaScript literal does not make it safe in every place it ends up. A value placed inside an HTML page, an inline script or an event handler needs the escaping rules of that context as well. The option that escapes < helps when a string sits inside a script tag, but treat user input as untrusted and use the framework's own encoding.

Error messages and what to do

MessageWhat it meansFix
Legacy octal escape "\01" at line 1, column 2 is not allowed in modern JavaScript strings. Use \xNN or \uNNNN instead.A backslash followed by digits, such as \012, is an old octal escape that strict mode and template literals reject.Replace it with \xNN, for example \x0a, or \uNNNN.
Incomplete \x escape at line 1, column 1: \x must be followed by exactly 2 hex digits.\x needs two hex digits after it and fewer were found.Add the missing digit, for example \x41, or remove the backslash.
The escape \u{110000} at line 1, column 1 is above U+10FFFF, the largest Unicode code point.The code point inside \u{...} is larger than Unicode allows.Use a value from 0 to 10FFFF.

Questions people ask

How do I escape quotes in a JavaScript string?

Choose the quote style your string uses and press Escape. Only that quote gets a backslash, so a double-quoted string leaves single quotes alone and the other way round. Tick Add surrounding quotes to get the complete literal.

How do I escape a template literal in JavaScript?

Pick Template literal as the quote style. Backticks are escaped as \` and a dollar sign followed by an opening brace is escaped as \${, so the text is not read as an expression. A dollar sign on its own is left alone.

How do I unescape a JavaScript string?

Paste the string, with or without its outer quotes, and press Unescape. Escapes such as \n, \x41, \u00e9 and \u{1F600} become the characters they stand for. Nothing is evaluated, and an invalid escape gives an error with its position.

What is the difference between \x and \u escapes?

\xNN takes exactly two hex digits and covers characters up to U+00FF. \uNNNN takes four and covers the Basic Multilingual Plane, while \u{...} takes any number of digits and reaches every code point, up to U+10FFFF. Characters beyond U+FFFF can also be written as two \u escapes.

Why are U+2028 and U+2029 escaped?

They are line separator characters. Before ES2019 a raw one inside a string literal was a syntax error, and some tools still treat them as line breaks. Writing them as escapes is always safe, so the tool does it by default.

Does this work for JSON strings too?

Mostly, but not exactly. JSON allows only double quotes and no \x or \v escapes. If the text will go into a JSON document, use the JSON escape tool on this site so the output follows the JSON standard.

About this tool

Escape handles backslashes, the quote you chose, line breaks, tabs, other control characters and the line separators U+2028 and U+2029. In template literal mode it also escapes backticks and ${. Unescape reads \n, \xNN, \uNNNN, \u{...} and line continuations, and accepts text with or without matching outer quotes. It rejects legacy octal escapes such as \012. It does not run or evaluate any JavaScript. Escaping a string is not enough to place untrusted input safely into HTML or an inline script.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.