UtilityKit

500+ fast, free tools. Most run in your browser only; Image & PDF tools upload files to the backend when you run them.

JavaScript String Escape

Encode and decode JavaScript escape sequences for safe string handling.

About JavaScript String Escape

JavaScript string escaping is a constant friction point when moving text between different contexts — copying a file path from Windows, embedding a SQL query inside a JS string, inserting a JSON value with quotes into source code, or preparing a string literal for a test fixture. A single unescaped backslash or quote breaks the string and causes a syntax error. JavaScript String Escape handles both directions: escape raw text so it is safe to embed inside a JavaScript string literal (adding backslashes before quotes, converting newlines to \n, backslashes to \\, and tabs to \t), and unescape a string literal that contains escape sequences back into the readable plain-text characters they represent. Supports standard JS escape sequences: \n, \r, \t, \\, \', \", \uXXXX (Unicode), \xXX (hex), and \0 (null). All operations run instantly in the browser.

Why use JavaScript String Escape

Eliminates Manual Backslash Counting

Automatically escapes every backslash, quote, newline, and tab in your input — no more manually adding \\ before each backslash in a file path or regex.

Both Directions Supported

Escape raw text for embedding in source code and unescape string literals to read their actual content — two operations in one tool without switching tabs.

Quote Style Aware

Choose single-quote or double-quote target context so only the relevant delimiter is escaped, keeping the output as clean as possible.

Unicode Escape Support

Optionally converts non-ASCII characters (accented letters, emoji, CJK symbols) to \uXXXX sequences for embedding in ASCII-only JS files or JSON.

Reliable Test Data Preparation

Prepare multiline SQL queries, file paths, regex patterns, or JSON payloads as escaped string literals ready to paste into unit test fixtures without syntax errors.

Fully Private Client-Side

All processing happens in your browser — passwords, API keys, connection strings, and other sensitive values you need to escape never leave your device.

How to use JavaScript String Escape

  1. Select the operation: Escape (add backslashes to make text safe for a JS string) or Unescape (convert \n, \t, \" sequences back to their real characters).
  2. Paste your input text into the input area — output updates immediately.
  3. Choose your target quote style: single quotes or double quotes, so the correct quote character is escaped.
  4. For escape mode, enable 'Escape Unicode' to convert non-ASCII characters to \uXXXX sequences for maximum ASCII compatibility.
  5. Review the output to verify all escape sequences are correct for your target context.
  6. Click Copy to copy the escaped or unescaped result to your clipboard.

When to use JavaScript String Escape

  • When embedding a multiline SQL query or text template as a JavaScript string literal and needing to escape all quotes and newlines in one step.
  • When copying a Windows file path (C:\Users\name\file.txt) into a JavaScript string and the backslashes need to be doubled to prevent escape sequence errors.
  • When preparing a regex pattern string that contains backslash sequences and the pattern needs to be stored as a string literal rather than a regex literal.
  • When writing unit test fixtures that include strings with quotes, newlines, or special characters and pasting them raw would cause a syntax error.
  • When reading a JavaScript string value from a log file or API response that contains escape sequences and you need to see the actual rendered string.
  • When constructing a JSON string value that must be embedded inside a JavaScript variable and both levels of quoting need to be correctly escaped.

Examples

Escape a Windows file path

Input: C:\Users\alice\Documents\report.pdf

Output: C:\\Users\\alice\\Documents\\report.pdf

Escape a multiline SQL query

Input: SELECT id, name FROM users WHERE status = 'active' ORDER BY name ASC;

Output: SELECT id, name\nFROM users\nWHERE status = \'active\'\nORDER BY name ASC;

Unescape a log-file string back to readable text

Input: Hello\tWorld\nLine 2: \"quoted value\"

Output: Hello World Line 2: "quoted value"

Tips

  • When embedding a Windows file path like C:\Users\name into a JS string, paste it into the escaper first — it doubles all backslashes automatically, far faster than editing manually.
  • Use single-quote mode when your codebase uses single quotes consistently — only the apostrophe is escaped, resulting in cleaner output that requires fewer backslashes than double-quote mode.
  • To embed a multiline SQL query as a JS string, write it in a text editor first, paste into the escaper, and copy the result — it converts all newlines to \n in one operation.
  • When debugging a string that displays as garbage characters, unescape it to see the original value — particularly useful for strings copied from log files that contain \t and \n sequences displayed literally.
  • For modern codebases, prefer template literals (backtick strings) for strings that contain quotes or newlines — they require far less escaping and are more readable than escaped regular strings.

Frequently Asked Questions

What characters does JavaScript string escaping handle?
The tool escapes: backslash \ (→ \\), double quote " (→ \"), single quote ' (→ \'), newline (→ \n), carriage return (→ \r), tab (→ \t), null character (→ \0), and optionally non-ASCII characters (→ \uXXXX). These cover all standard JavaScript string escape sequences defined in the ECMAScript specification.
What is the difference between \n and a real newline in a JS string?
A real newline character (Unicode U+000A) in a JavaScript source string causes a syntax error in a regular single or double-quoted string — the string literal must be on one line. The escape sequence \n inside a quoted string tells JavaScript to interpret those two characters as a single newline character. Template literals (backtick strings) are the exception — they allow real newlines in the source.
When would I use \uXXXX vs. typing the character directly?
Type characters directly when your source file is UTF-8 encoded and your team's tools and editor support it — this is the modern standard. Use \uXXXX sequences when the file must be ASCII-only (some legacy build systems, certain minifiers, or environments that mangle non-ASCII bytes), when embedding the string in a JSON file that will be processed by a limited parser, or when the character is invisible/zero-width and you want the escape to be explicit.
Does this tool handle template literal escaping?
Template literals (backtick strings) require escaping backticks (\`) and dollar-brace sequences (\${). The tool's standard escape mode covers the common characters. If you specifically need backtick escaping, ensure your escaped output replaces ` with \` before using it inside a template literal.
How do I safely embed a JSON string inside a JavaScript variable?
If you assign a JSON string to a JS variable using double quotes, any double quotes inside the JSON must be escaped: const data = "{\"key\": \"value\"}". Alternatively, use JSON.stringify(obj) to produce the string programmatically, or use a template literal with the raw JSON pasted inside backticks (which handles most characters without manual escaping).
What is \x vs. \u in JavaScript escape sequences?
\xXX represents a character by its two-digit hexadecimal code point in the Latin-1 range (U+0000–U+00FF). \uXXXX represents any Unicode character by its four-digit hex code point (U+0000–U+FFFF). For characters above U+FFFF (such as emoji in the supplementary planes), use surrogate pairs (\uD83D\uDE00) or the ES6 form \u{1F600}.
Is there a difference between escaping for JSON vs. JavaScript?
JSON only permits double-quoted strings and has its own set of mandatory escapes: \" \\ \/ \b \f \n \r \t and \uXXXX. JSON does not allow single quotes or \x escapes. If you need to produce a valid JSON string value, use this tool with double-quote mode and also escape forward slashes if required by your JSON parser.
Can I unescape a string that contains \u0000 (null character)?
Yes. The unescaper converts \0 and \u0000 to the actual null byte (U+0000). Be aware that some string operations and languages treat null bytes as string terminators — if your downstream system is one of them, the null character may cause truncation.

Explore the category

Glossary

Escape Sequence
A combination of characters beginning with a backslash that represents a single character in a string literal. JavaScript escape sequences include \n (newline), \t (tab), \\ (backslash), \" (double quote), and \uXXXX (Unicode code point).
String Literal
A value directly written in source code as a quoted sequence of characters: 'hello', "world", or `template`. The literal syntax determines which characters require escaping.
Template Literal
A JavaScript string delimited by backtick characters (`) instead of quotes. Template literals support multi-line strings and embedded expressions (${expr}) without requiring newline escaping.
Unicode Escape (\uXXXX)
A JavaScript escape sequence that represents a Unicode character by its four-digit hexadecimal code point. Useful for embedding non-ASCII characters in ASCII-only source files.
Surrogate Pair
A pair of UTF-16 code units used to represent Unicode characters above U+FFFF (the supplementary planes). In JavaScript strings, emoji and some rare scripts are stored as surrogate pairs: \uD83D\uDE00 represents 😀.
Raw String
A string in which escape sequences are not interpreted — backslashes are treated as literal characters. In JavaScript, String.raw`C:\Users\name` produces C:\Users\name without processing the backslashes.