JavaScript Escape

Escape special characters in JavaScript strings.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What JavaScript Escape does

JavaScript Escape prepares text for a JavaScript string literal. It escapes the backslash, all three quote characters (', " and `) and the ${ that would start a placeholder, so the result is safe inside single quotes, double quotes or a template literal.

Control characters get their short escapes where JavaScript has one (\n, \t, \v, \0 and so on) and \xHH otherwise. Other characters, including accented letters and emoji, are kept as typed.

JavaScript Escape examples

The built-in sample

Input

say "hello" & <b>bye</b>

Output

say \"hello\" & <b>bye</b>

This is the input that the Load sample button fills in.

Quotes and a template placeholder

Input

It's "${name}" `now`

Output

It\'s \"\${name}\" \`now\`

Every quote kind is escaped, and so is the $ before {, so the text stays literal even inside backticks.

Control characters

Input

a	b
c␀d␁ [U+2028]

Output

a\tb\nc\0d\x01 \u2028

U+0000 becomes \0, U+0001 becomes \x01, and U+2028 (line separator, which ended a string literal in engines before ES2019) becomes \u2028.

Each output was produced by running this tool on the input above it. Invisible control characters are shown as symbols, for example ␁ for U+0001 and ␀ for U+0000.

How it works

Each character is checked against the rules below; all other characters are copied unchanged.

CharacterWritten as
\\\
' " `\' \" \`
$ before {\$
line break, carriage return, tab\n \r \t
backspace, form feed, vertical tab\b \f \v
U+0000\0 (or \x00 before a digit)
other control characters\x01 … \x1F, \x7F
U+2028, U+2029\u2028 \u2029

Details and limits

  • The output is for string literals. It is not a way to make text safe inside an HTML <script> element: there, </script> ends the element whatever the escaping. Use JSON.stringify and encode < when you embed data in a page.
  • For JSON, use JSON Escape: JSON does not accept \', \x or \v.

Questions

Is my data uploaded when I use JavaScript Escape?

No. The tool runs in your browser, so what you paste or open is processed on your own device and is not sent to a server.

What does JavaScript Escape do?

JavaScript Escape prepares text for a JavaScript string literal. It escapes the backslash, all three quote characters (', " and `) and the ${ that would start a placeholder, so the result is safe inside single quotes, double quotes or a template literal.

Are there any limits I should know about?

The output is for string literals. It is not a way to make text safe inside an HTML <script> element: there, </script> ends the element whatever the escaping. Use JSON.stringify and encode < when you embed data in a page. For JSON, use JSON Escape: JSON does not accept \', \x or \v.

Is there a size limit?

There is no fixed size limit. Everything happens on your device, so very large inputs depend on its memory and can take longer on a phone than on a computer.

Does it work on phones, and is it free?

Yes. It works in any current browser on Android phones, iPhones, tablets and computers. It is free and needs no account or sign-up.