JavaScript String Escape / Unescape
Escape and unescape JavaScript strings — single quotes, double quotes, template literals, Unicode, hex
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
| Sequence | Represents |
|---|---|
| \' | Single quote |
| \" | Double quote |
| \\` | Backtick |
| \\ | Backslash |
| \n | Newline (LF) |
| \r | Carriage return (CR) |
| \t | Horizontal tab |
| \b | Backspace |
| \f | Form feed |
| \0 | Null character |
| \xNN | Hex escape (Latin-1) |
| \uXXXX | Unicode 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:
| Sequence | Represents | Notes |
|---|---|---|
\' | Single quote | Only required in single-quoted strings |
\" | Double quote | Only required in double-quoted strings |
\` | Backtick | Only required in template literals |
\\ | Backslash | Always required |
\n | Newline (LF, U+000A) | |
\r | Carriage return (CR, U+000D) | |
\t | Horizontal tab (U+0009) | |
\b | Backspace (U+0008) | |
\f | Form feed (U+000C) | |
\0 | Null character (U+0000) | Avoid in non-null terminated contexts |
\xNN | Hex escape (Latin-1, U+0000–00FF) | e.g. \xA9 for © |
\uXXXX | Unicode 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:
- Minified JavaScript source code
- Log file output from Node.js error messages
- JSON payloads that contain escaped JavaScript within them
- Database columns storing escaped JavaScript snippets
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:
\`→ literal backtick\${→ literal${without starting a template expression\\→ literal backslash (as always)
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
- JSON Escape / Unescape — escape strings for JSON contexts (stricter rules)
- URL Encoder / Decoder — percent-encode strings for URLs
- HTML Entity Encoder — escape
<,>,&,"for HTML - Regex Tester — test and debug JavaScript regular expressions