Skip to content

JavaScript Escape Generator

Escape text for a JavaScript string literal, including the </script> case.

Runs in your browserNo account neededFree
Loading tool…

Processing: This tool runs entirely in your browser. Your input and any file you open stay on your device — nothing is uploaded to a server.

How to use the javascript escape generator

  1. Paste your text.
  2. Choose the quote style.
  3. Copy the escaped string or the complete literal.

About this tool

Quotes, backslashes and newlines all need escaping in a JavaScript string literal, and the tool handles whichever quote style you are using — including backticks, where a <code>${</code> would otherwise start an interpolation.

The case worth understanding is <code>&lt;/script&gt;</code>. It is not special in JavaScript at all, but an HTML parser looking for the end of a script element does not know or care that it is inside a string. A raw <code>&lt;/script&gt;</code> ends the element there, and the remainder of your string becomes page content — which is how an ordinary string turns into an injection. It is escaped here as <code>\\x3C</code>, which JavaScript reads identically and the HTML parser does not recognise.

The line separator characters U+2028 and U+2029 are escaped for a related historical reason: they were illegal in string literals for years and still break older engines.

The output is checked by parsing it back and comparing with your original, so correctness is demonstrated rather than asserted.

Common uses

  • Embedding text in a script tag from a template.
  • Putting multi-line text into a string literal.
  • Escaping content that contains quotes or HTML.

Frequently asked questions

Why is </script> escaped when it is not special in JavaScript?
Because an HTML parser ends the script element at that sequence regardless of any string around it. Escaping it as \x3C keeps the string identical to JavaScript and invisible to the HTML parser.
Do backticks need different escaping?
Yes. In a template literal, ${ starts an interpolation, so it needs escaping in addition to the backtick itself.
Should I use this instead of JSON.stringify?
JSON.stringify is fine in code and handles most cases. It does not escape </script>, which matters when writing into an HTML page.

Related tools

sequence that ends a script element.","applicationCategory":"UtilityApplication","operatingSystem":"Any modern web browser","browserRequirements":"Requires JavaScript","offers":{"@type":"Offer","price":"0","priceCurrency":"USD"},"publisher":{"@id":"https://webkitool.com/#organization"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Why is escaped when it is not special in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Because an HTML parser ends the script element at that sequence regardless of any string around it. Escaping it as \\x3C keeps the string identical to JavaScript and invisible to the HTML parser."}},{"@type":"Question","name":"Do backticks need different escaping?","acceptedAnswer":{"@type":"Answer","text":"Yes. In a template literal, ${ starts an interpolation, so it needs escaping in addition to the backtick itself."}},{"@type":"Question","name":"Should I use this instead of JSON.stringify?","acceptedAnswer":{"@type":"Answer","text":"JSON.stringify is fine in code and handles most cases. It does not escape , which matters when writing into an HTML page."}}]}]}