PureDevTools

JavaScript String Escape / Unescape

Escape and unescape JavaScript strings — single quotes, double quotes, template literals, Unicode, hex

All processing happens in your browser. No data is sent to any server.

Quote Style

Escapes double quotes. Use for: const s = "…"

Enter any text above to escape special characters for use inside a JavaScript string literal.

JavaScript Escape Sequences Reference
SequenceRepresents
\'Single quote
\"Double quote
\\`Backtick
\\Backslash
\nNewline (LF)
\rCarriage return (CR)
\tHorizontal tab
\bBackspace
\fForm feed
\0Null character
\xNNHex escape (Latin-1)
\uXXXXUnicode escape (BMP)
\u{XXXXXX}Unicode code point escape

You are hardcoding a user-facing message in a JavaScript file. The message contains single quotes, double quotes, and a newline — and you are not sure which characters need escaping for which quote style. This tool takes the raw string, lets you choose your quote style, and produces the correctly escaped output instantly. No guessing, no syntax errors at runtime.

JavaScript String Literals: Three Quote Styles

JavaScript supports three ways to delimit string literals, each with different escaping requirements:

Double-Quoted Strings ("…")

Double quotes must be escaped as \". Backslashes must be escaped as \\. Single quotes can appear literally without escaping.

const s = "She said \"hello\" and it's fine.";

Single-Quoted Strings ('…')

Single quotes must be escaped as \'. Backslashes must be escaped as \\. Double quotes can appear literally.

const s = 'She said "hello" and it\'s fine.';

Template Literals (`…`)

Backticks must be escaped as \`. The ${ sequence must be escaped as \${ to prevent accidental template expression start. Backslashes must be escaped as \\. Both single and double quotes are literal.

const s = `She said "hello" and it's \`great\`. Cost: \${price}`;

Escape Sequences in JavaScript

JavaScript string literals share most escape sequences with JSON but add several more:

SequenceRepresentsNotes
\'Single quoteOnly required in single-quoted strings
\"Double quoteOnly required in double-quoted strings
\`BacktickOnly required in template literals
\\BackslashAlways required
\nNewline (LF, U+000A)
\rCarriage return (CR, U+000D)
\tHorizontal tab (U+0009)
\bBackspace (U+0008)
\fForm feed (U+000C)
\0Null character (U+0000)Avoid in non-null terminated contexts
\xNNHex escape (Latin-1, U+0000–00FF)e.g. \xA9 for ©
\uXXXXUnicode escape (BMP, U+0000–FFFF)e.g. \u00e9 for é
\u{XXXXXX}Unicode code point escape (ES6+)Supports full U+000000–U+10FFFF

When You Need JavaScript String Escaping

Dynamic string construction: When you build JavaScript source code programmatically (in a code generator, template engine, or CLI tool), you must escape string values that will be embedded in the generated code.

Hardcoded data in source files: Error messages, UI labels, SQL query templates, regex patterns, and content that contains quotes or special characters must be escaped for the quote style you chose.

eval and Function constructor: Code passed to eval() or new Function() is parsed as JavaScript source, so string values inside it must be properly escaped.

JSON-in-JS patterns: When you write const data = { key: "value with \"quotes\"" } directly in a .js file, the JSON escaping rules apply to the string value.

Internationalization (i18n) strings: Translation strings for English content often contain apostrophes (') that conflict with single-quoted JavaScript strings.

Hex Escapes vs Unicode Escapes

JavaScript supports two forms of Unicode escape in string literals:

\xNN (hex escape): Represents code points U+0000 through U+00FF. Two hex digits. Convenient for Latin-1 characters: \xa9 (©), \xe9 (é), \xfc (ü).

\uXXXX (Unicode escape): Represents code points U+0000 through U+FFFF using exactly four hex digits. Works for any character in the Basic Multilingual Plane.

\u{XXXXXX} (ES2015 Unicode code point escape): Supports the full Unicode range U+000000 to U+10FFFF. Required for characters outside the BMP such as emoji: \u{1F680} (🚀). Supported in Node.js 4+ and all modern browsers.

Unescaping JavaScript Strings

The unescape direction accepts a string containing JavaScript escape sequences and converts them back to the original characters. It handles all escape forms: \n, \t, \', \", \\, \xNN, \uXXXX, and \u{XXXXXX}.

This is useful when you encounter escaped strings in:

Template Literals: What Gets Escaped

Template literals (backtick strings) are parsed differently from regular strings. The parser looks for ${ to start an expression and ` to end the literal. The escape rules are:

Everything else (single quotes, double quotes, newlines, Unicode) can appear literally in a template literal without escaping. This makes template literals the most permissive quote style for embedded text.

Practical Examples

Embedding a Windows path:

// Raw: C:\Users\name\file.txt
const path = "C:\\Users\\name\\file.txt"; // double-quoted
const path2 = 'C:\\Users\\name\\file.txt'; // single-quoted
const path3 = `C:\\Users\\name\\file.txt`; // template literal

Embedding HTML in JavaScript:

const html = "<div class=\"container\">Hello</div>"; // double-quoted
const html2 = '<div class="container">Hello</div>';   // single-quoted (no escaping needed)
const html3 = `<div class="container">Hello</div>`;  // template literal (no escaping needed)

Multi-line string (before template literals):

const msg = "Line 1\nLine 2\nLine 3";

Frequently Asked Questions

Should I use single quotes, double quotes, or template literals? Most JavaScript style guides recommend consistency throughout a project. ESLint’s quotes rule can enforce your preferred style. Template literals are preferred when the string contains the other quote type or when you need multi-line strings. Single quotes are popular in Node.js ecosystem; double quotes are common in Java/C backgrounds.

Do I need to escape forward slashes in JavaScript strings? No. / has no special meaning inside JavaScript string literals. You only need to escape it in regex literals (/pattern/) where / ends the literal.

What is the difference between \n and a literal newline in a template literal? In a regular string ("…" or '…'), a literal newline character is a syntax error — you must use \n. In a template literal (`…`), a literal newline is preserved as-is in the string value. Both \n and a real line break produce the same string value inside a template literal.

Is this tool safe for sensitive values like passwords or API keys? Yes. All processing happens locally in your browser. No data is transmitted to any server.

Related Tools

More Encoding & Crypto Tools