Format Code Now
/
Encode & decodeJavaScript Escape / Unescape Next: JSON Encode ›

JavaScript Escape / Unescape

ED-57

Escape text for a JavaScript string, unescape it again, or reproduce what the legacy escape() and unescape() functions return.

Runs in your browser. Nothing is uploaded.

Text, string literal content or %-escaped text

Result

How to use it

  1. Paste your text, or the contents of a string literal, into the input box.
  2. For string work, choose the quote style and options, then click Escape string or Unescape string.
  3. For the global functions, click escape() or unescape(). Those ignore the string options.
  4. Copy the result or download it as a .txt file.

Two different things called escape

Escaping a string means adding backslashes so text fits inside quotes: \n for a line break, \' for a quote, \uNNNN for other characters. The old global function escape() is something else. It percent-encodes text: characters up to 255 become %XX and everything higher becomes %uXXXX. This page has both, in separate buttons, so you can see the difference on the same input.

String escape and unescape

Escape handles backslashes, your quote style, line breaks, control characters and the line separators U+2028 and U+2029. Template literal mode also escapes backticks and ${. Unescape reads \n, \xNN, \uNNNN, \u{...} and line continuations, and rejects legacy octal escapes such as \012. Nothing is evaluated, so pasted code is never run.

The legacy functions

escape() leaves letters, digits and the characters @ * _ + - . / alone and encodes the rest. It works on UTF-16 code units, so an emoji becomes two %u escapes. unescape() turns %XX and %uXXXX back and leaves a % that is not followed by valid hex as typed. Both functions are deprecated and are not URL encoding; for URLs use encodeURIComponent, which is available on this site as the URL encoder.

Error messages and what to do

MessageWhat it meansFix
unpaired surrogateThe result of unescaping would contain half of an emoji, such as a lone %uD83D.Include the matching low surrogate, for example %uDE00 after %uD83D.

Questions people ask

How do I escape a string in JavaScript?

Paste the text, pick single, double or template quotes, and click Escape string. Backslashes, the chosen quote, line breaks and control characters get a backslash escape, so the text can be placed inside a string literal safely.

What does JavaScript escape() do?

The legacy escape() function turns each character into %XX if its code is 255 or lower, or %uXXXX if it is higher, except letters, digits and @ * _ + - . /. Click escape() here to see it on your text.

Is escape() the same as encodeURIComponent?

No. encodeURIComponent writes non-ASCII characters as UTF-8 percent sequences, while escape() writes Latin-1 characters as one byte and others as %uXXXX. escape() is deprecated and gives results other systems cannot read.

How do I unescape a JavaScript string?

Paste the escaped text and click Unescape string. Sequences such as \n, \x41, \u00e9 and \u{1F600} are turned into real characters. Surrounding quotes are accepted but not required.

Does escaping make text safe for HTML?

No. String escaping only prepares text for a JavaScript literal. Placing untrusted input into HTML or an inline script needs HTML encoding for that context, and the Escape < option helps only inside script tags.

Why does unescape() leave some percent signs alone?

A % that is not followed by two hex digits, or by u and four hex digits, is not an escape, so unescape() leaves it as written, the same way the browser function does. For example 100% stays 100%.

About this tool

The two string actions work on the inside of a JavaScript string literal: escape handles backslashes, your quote style, line breaks, control characters and the separators U+2028 and U+2029, and unescape reads \n, \xNN, \uNNNN, \u{...} and line continuations. The other two actions mimic the old global functions escape() and unescape(). They write characters up to 255 as %XX, others as %uXXXX, and leave letters, digits and @ * _ + - . / alone. Those functions are deprecated and are not URL encoding; use encodeURIComponent for URLs. Unescape leaves a % that is not followed by valid hex as typed. Nothing is evaluated or run.

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