Why escape text in JavaScript?
Escaping text in JavaScript is necessary when you want to include special characters in a string. For example, if you want to use a double quote in a string that is already enclosed in double quotes, you need to escape it with a backslash.
Examples of JavaScript escaping
Original text
Hello "World"
Line with 'single' quotes
Tab and newline
characters
Backslash \ and other special charsEscaped text
\bHello\b \"\bWorld\b\"\\bnLine\b \bwith\b \'\bsingle\b\' \bquotes\b\\bnTab\b\\btand\b \bnewline\b\\bncharacters\b\\bnBackslash\b \\ \band\b \bother\b \bspecial\b \bchars\bCommon escape sequences in JavaScript
| Character | Escape Sequence | Description |
|---|---|---|
| " | \" | Double quote |
| ' | \' | Single quote |
| \ | \\ | Backslash |
| New line | \n | Line feed |
| Carriage return | \r | Carriage return |
| Tab | \t | Horizontal tab |
| Form feed | \f | Form feed |
| Backspace | \b | Backspace |
| Vertical tab | \v | Vertical tab |
| Null character | \0 | Null character |
When to use JavaScript escaping
- When creating string literals in JavaScript code
- When generating JavaScript code dynamically
- When working with JSON data that contains special characters
- When storing text that will be evaluated as JavaScript later
- When displaying code examples that contain JavaScript strings
This tool is useful for preparing text to be used in JavaScript code, JSON data, or any context where JavaScript string escaping rules apply.
Tip:
In modern JavaScript, you can also use template literals (backticks) to avoid escaping quotes: `Hello "world" with 'quotes'`. However, you'll still need to escape backticks and some special characters within template literals.
Related Tools
- Unescape JavaScript - Convert escaped JavaScript strings back to their original form
- Escape HTML - Escape text for use in HTML
- Escape JSON - Escape text for use in JSON strings
- Escape Java - Escape text for use in Java strings
- URL Encode - Encode text for use in URLs
All Tools
See all available tools