jsonecho

JavaScript Escape

Paste your text and get it escaped for JavaScript, ready to paste into code or data.

Loading the tool...

How to use JavaScript Escape

  1. Paste or type the text you want to escape.
  2. Adjust the options if you need to.
  3. Copy the escaped result.

About JavaScript Escape

A JavaScript string literal cannot contain a raw line break or an unescaped quote of its own kind. If you build a string from user input or paste a block of text into code, the quotes and backslashes have to be escaped, otherwise the script stops with a syntax error, or worse, runs something you did not intend.

The tool escapes backslashes, both kinds of quotes, line breaks, tabs and other control characters, so the result is safe inside single or double quoted strings. Template literals are a different story: they need only backticks and ${ escaped.

What gets escaped

Backslash becomes \\, quotes become \" and \', a line break becomes \n, a tab \t, other control characters \xNN.

Frequently asked questions

Does it work for template literals?

Partly. The result is valid inside them, but backticks and ${ are not escaped. If your text contains those, escape them by hand.

Why is the forward slash option there?

Escaping / as \/ prevents a closing script tag inside a string from ending an HTML script block early. It is only needed if you put the JavaScript inside HTML.

What are U+2028 and U+2029?

Line and paragraph separators. Before ES2019 they were illegal in string literals, so this tool always escapes them.

Should I escape non-ASCII characters?

Usually not, modern JavaScript files are UTF-8 and happily hold é or 日本語. Turn the option on only when the file travels through something that might mangle encodings.

More in Escape and Text

All tools →