CalcCafe

JavaScript Escape

Escape text into a JavaScript string literal (quotes, backslashes, newlines). It runs entirely on your device — nothing is uploaded.

Example

Input
He said "hi"
bye
Output
He said \"hi\"\nbye

How it works

Your text is escaped using JavaScript string rules so it can be embedded safely between quotes.

Good to know

JavaScript Escape takes any raw text you paste in and converts it into a safe JavaScript string literal — escaping the characters that would otherwise break your code if you dropped the text between quotes. That means double quotes become \", backslashes become \\, line breaks become \n, tabs become \t, and so on. It's aimed at developers who need to hard-code a chunk of text, a message, a regex source, or a snippet of HTML into a .js file without manually hunting for every character that needs a leading backslash.

You'll reach for this when you're embedding multi-line content (an email template, a SQL query, an error message) into a string, building a JSON payload by hand, or pasting copied text that contains quotes you know will cause a syntax error. Instead of fixing the resulting "unterminated string literal" errors one by one, you escape the whole block in one pass and paste the result straight between your quotes.

To read the output: every backslash you see was added to neutralize a special character, so the escaped string is meant to be wrapped in quotes and assigned, not displayed as-is. When your program later prints or uses that string, the escapes resolve back to the original characters — \n becomes a real newline again, \" becomes a plain quote. If the output still looks like literal backslashes after running, that's expected; the runtime interprets them, not your editor.

A practical caveat: pick the same quote style in your code that the tool assumes. This tool escapes double quotes, so it's built for wrapping in "..." or backticks; if you paste the result inside single quotes you may still hit an unescaped apostrophe. For round-tripping, pair it with the JavaScript Unescape tool to confirm your escaped text decodes back to exactly what you started with.

Frequently asked questions

Is my data uploaded anywhere?
No — everything runs in your browser. Your code never leaves your device, so it's safe for private work and runs offline once loaded.
Is this tool free?
Yes, completely free with no sign-up and no limits.

People also ask

What characters need to be escaped in a JavaScript string?
The core ones are the backslash itself (\\), the quote character matching your string delimiter (\" or \'), and whitespace control characters like newline (\n), carriage return (\r), and tab (\t). Backticks and ${ also matter inside template literals.
What is the difference between escaping for a JavaScript string and JSON?
They overlap heavily but are not identical. JSON requires double quotes and forbids things like single-quote escaping, trailing commas, and certain unescaped control characters, while JavaScript string literals are more permissive and also support single quotes, template literals, and escapes like \v and \0.
How do I escape a newline in a JavaScript string?
Replace the actual line break with the two-character sequence \n (backslash followed by n). When the string is evaluated, \n is interpreted as a single newline character again.
Can I put a multi-line string in JavaScript without escaping it?
Yes, by using template literals with backticks, which allow real line breaks inside the string. Regular single- or double-quoted strings cannot span multiple lines, so those require escaping the newlines as \n.
How do I reverse JavaScript escaping?
Use an unescape tool or evaluate the string in a JavaScript runtime, which converts sequences like \n, \t, and \" back to their original characters. CalcCafe offers a separate JavaScript Unescape tool for this.
Is it safe to use escaped strings to insert text into HTML?
JavaScript string escaping makes text safe to sit inside a string literal, but it does not make it safe for HTML output. Inserting user text into a page still requires HTML-context escaping or safe DOM methods to prevent cross-site scripting.
Why does my escaped string show double backslashes?
A double backslash (\\) is the correct escape for a single literal backslash in the source. When the program runs, each \\ resolves to one backslash, so seeing two in the escaped text is expected, not a bug.

Related tools