Escape text for safe use in JavaScript string literals

Escape text so it can appear safely inside a JavaScript string literal.

freeworks offlinenothing uploaded
ToolJavaScript Escape
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

Characters with syntactic meaning in JavaScript string literals are replaced with escape sequences such as backslash, quote, newline, or Unicode code-unit escapes. The result is source text, not a JavaScript value with executable behavior.

  • Escapes preserve delimiters and control characters under ECMAScript lexical grammar.

Worked example

Escape Quotes
Escape special characters for use in a JavaScript string literal
Input
											He said "hello"
New line
										
Output
												He said \"hello\"\nNew line
											

When to use this

Templates, inline-script fixtures, and serializers escape JavaScript string values.

Edge cases

  • A backslash must be escaped before a quote is considered safe.
  • U+2028 and U+2029 need care in older script embedding contexts.
  • JavaScript escaping is not the same as HTML-attribute escaping.

References