The JS String Escaper on FreeToolZones turns plain text into a valid JavaScript string literal, complete with quotes, ready to paste directly into your code. If you have ever tried to embed a sentence containing quotation marks, a line break, a tab, or a backslash inside a JavaScript string, you know how fiddly it gets: one unescaped quote ends the string early, one raw line break breaks the syntax, and one stray backslash changes the meaning of the next character. This tool handles all of that for you. You type or paste the text, choose a quote style, press Escape, and the result is a string literal you can drop into a script with confidence.
String escaping is one of those small tasks that appears constantly in real development and is easy to get subtly wrong by hand. Text copied from a document, a spreadsheet, a chat message, or a user report often contains characters that are special inside a string. Doing the escaping by hand means scanning every character, remembering which ones need a backslash, and hoping you did not miss one. The escaper removes that guesswork. It applies the same rules every time, it shows you the exact literal, and it lets you copy the result in one click.
What Is the JS String Escaper?
The JS String Escaper is a free online tool that converts ordinary text into a JavaScript string literal. A string literal is text wrapped in quotes, with special characters replaced by escape sequences that the JavaScript engine understands. For example, a line break inside your text becomes the two characters backslash and n, a tab becomes backslash and t, and a backslash itself becomes two backslashes. When JavaScript reads the literal, it converts those escape sequences back into the original characters, so the string value at runtime is exactly the text you started with.
The tool offers three quote styles. Single quotes wrap the text in apostrophes, double quotes wrap it in quotation marks, and template literal style wraps it in backticks. The choice matters because the tool escapes the quote character you chose: with single quotes selected, apostrophes in your text are escaped, while double quotes are left alone, and vice versa. With template literal style, backticks are escaped, and the two-character sequence dollar sign followed by an opening brace is also escaped, so that a piece of text like a placeholder is not mistaken for a template expression when the literal runs.
Beyond quotes, the tool escapes backslashes, line breaks, carriage returns, tabs, backspaces, form feeds, and vertical tabs, each with its standard short escape sequence. Control characters and the delete character, which have no visible form, are written as hexadecimal escapes with two digits. The null character gets special treatment: it becomes backslash-zero, unless the next character is a digit, in which case it becomes a hexadecimal escape, so the engine cannot misread it as a longer numeric escape. Characters outside the basic ASCII range are normally left as they are, so text in other languages and symbols stay readable in the output.
An optional checkbox changes that last behavior. When Escape non-ASCII characters as uXXXX is ticked, every character above the ASCII range is written as a four-digit Unicode escape, and characters beyond the basic multilingual plane, such as many emoji, are written as a pair of surrogate escapes. The resulting literal contains only ASCII characters, which is useful when a file, a transport channel, or a legacy tool cannot handle non-ASCII text reliably. The checkbox is off by default, because readable output is usually preferable.
How to Use the JS String Escaper
Escaping text takes four steps.
- Enter your plain text. Click in the box labeled Plain text and type or paste the text you want to turn into a string literal. The box accepts multiple lines, tabs, quotes, and backslashes without any preparation. The hint below the box reminds you that the output is a valid literal with quotes included, ready to paste into your code.
- Choose a quote style. Use the Quote style menu to pick Single quotes, Double quotes, or Template literal. Match the style your code already uses, or pick the style that requires the fewest escapes for your particular text: if the text contains many apostrophes, double quotes may be tidier, and if it contains many double quotes, single quotes may be tidier.
- Decide about non-ASCII escaping. Leave the checkbox unticked for normal use, so non-ASCII characters stay readable. Tick Escape non-ASCII characters as uXXXX if you need pure ASCII output, for example when pasting into a file whose encoding you do not control.
- Press Escape and copy the result. Click the Escape button. If the box is empty, the tool asks you to enter some text first. Otherwise the escaped literal appears below, with a Copy button. Press Copy, then paste the literal into your code. The Load sample button fills the box with example text containing quotes, an apostrophe, a line break, a tab, and a backslash, so you can see each kind of escape in one result.
After pasting the literal into your code, it is worth a quick check. Paste the surrounding line into the JavaScript Validator to confirm the syntax is valid, or run a small test in the JS Snippet Tester that prints the string and confirms it shows the original text. That check takes seconds and catches the rare case where surrounding code, rather than the literal itself, causes a problem, such as pasting a template literal into a context that does not support backticks.
Key Features of the JS String Escaper
The tool is small, and each feature maps directly to a control on the page.
Three quote styles
Single quotes, double quotes, and template literals cover the three ways JavaScript writes strings. The tool escapes the chosen quote character wherever it appears in the text, escapes backslashes first so they cannot interfere, and, for template literals, escapes the placeholder opener so literal dollar-brace text stays literal. You never have to remember which characters are special in which style.
Complete escape coverage for control characters
Line breaks, carriage returns, tabs, backspaces, form feeds, and vertical tabs all receive their standard short escapes. Other control characters and the delete character receive hexadecimal escapes. The null character is handled with the digit-aware rule described above. This coverage means text containing invisible characters, such as content pasted from a terminal or a data file, becomes a literal that behaves correctly.
Optional Unicode escaping
The non-ASCII checkbox converts every character above the ASCII range into Unicode escapes, including surrogate pairs for characters outside the basic multilingual plane. The result is pure ASCII, which survives encoding conversions, email transport, and tools that mangle non-ASCII bytes. When the box is unticked, the same characters stay as readable text, which is easier to review.
Quoted, copy-ready output
The output includes the surrounding quotes, so it is a complete literal rather than a fragment. You paste it as one unit, with no need to add quotes yourself and no risk of forgetting one. The Copy button puts the exact output on your clipboard.
Practical Use Cases
Escaping comes up whenever real-world text meets source code. The examples below cover the most common situations.
Embedding a message with quotes
Suppose your code needs to display the sentence: She said, “It is ready.” If you paste that sentence into a double-quoted string by hand, the inner quotes end the string early and the code breaks. The escaper with double quotes selected produces a literal where the inner quotes are escaped, or you can choose single quotes and let the double quotes stand untouched. Either way, the result is correct on the first try.
Embedding multi-line text
Help text, email templates, and error messages often span several lines. A raw line break inside a single-quoted or double-quoted string is a syntax error in JavaScript, so each break must become an escape sequence. The escaper converts every line break for you, preserving blank lines and the exact layout of the original text. For template literals, line breaks are legal as they are, but the escaper still converts them to escape sequences, which keeps the behavior identical and the literal on one logical line.
Embedding text with backslashes
File paths on Windows, regular expression patterns, and text copied from documentation often contain backslashes. Inside a string literal, a backslash starts an escape sequence, so each literal backslash must be doubled. The escaper doubles every backslash, so a path or a pattern survives the round trip unchanged. This is one of the easiest mistakes to make by hand and one of the most satisfying to automate.
Preparing test data
When you write tests, you often need string values that contain awkward characters: tabs, line breaks, quotes, and control characters. Generating those literals with the escaper guarantees the test data is exactly what you intended, instead of a near-miss caused by a missed escape. The same applies to seed data, sample payloads, and fixtures that must match real input byte for byte.
Creating ASCII-safe literals
If a file must contain only ASCII characters, because of an encoding restriction or a tool that corrupts other bytes, the Unicode escaping option produces a literal that is safe anywhere. The string value at runtime is identical to the original text, including characters outside the basic multilingual plane, because the surrogate pair escapes reconstruct them exactly.
Tips and Best Practices
- Pick the quote style that escapes least. If your text is full of apostrophes, use double quotes. If it is full of double quotes, use single quotes. Fewer escapes mean a literal that is easier to read and review.
- Prefer template literals for text with both quote types. When text contains apostrophes and double quotes, backticks avoid escaping either, and the tool handles the backtick and placeholder cases for you.
- Keep the Unicode option off unless you need it. Readable text is easier to verify. Turn ASCII-only output on only when encoding safety is the priority.
- Verify with a print test. After pasting a literal, print it and compare it with the original text. The JS Snippet Tester is ideal for this: one console line confirms the round trip.
- Do not escape twice. If text is already escaped, running it through the escaper again will escape the backslashes, changing the meaning. Use the JS String Unescaper to inspect escaped text instead.
- Watch for invisible characters. Text pasted from other programs can contain tabs, carriage returns, or control characters you cannot see. The escaper makes them visible as escape sequences, which is a good moment to decide whether they belong in your string at all.
Limitations
The escaper converts text into a literal, and that is all it does. It does not check whether the literal makes sense in the surrounding code, and it does not know whether you intended the text as a message, a pattern, or a path. It always escapes line breaks, even in template literals where raw line breaks would be legal, which keeps the output uniform but means the literal does not preserve the visual line layout of the original text in your editor. It does not escape forward slashes, because they need no escaping in JavaScript strings, and it does not add any surrounding code, such as a variable declaration: you get the literal alone, ready to place where you need it.
The tool also requires non-empty input. An empty box produces a message asking you to enter some text first, rather than an empty literal, which is a deliberate guard against accidental clicks. For very long text, the output is a single long literal, which is correct but may be unwieldy in an editor; splitting long text into several concatenated literals remains a manual decision. Finally, the tool runs in the browser and handles one input at a time, so bulk conversion of many strings is better done with a script.
Frequently Asked Questions
Why does the output include quotes?
Because the output is meant to be a complete string literal you can paste directly into code. Including the quotes means you paste one unit and cannot forget a closing quote. If you need the escaped content without quotes, copy the output and delete the first and last characters.
Which quote style should I use?
Use the style your project already uses for consistency, or the style that requires the fewest escapes for your text. All three styles produce the same string value; only the appearance of the literal differs.
What does the non-ASCII checkbox do?
It converts every character above the ASCII range into Unicode escapes, producing output that contains only ASCII characters. That is useful when a file or channel cannot handle non-ASCII text reliably. With the box unticked, those characters stay readable in the output.
How do I reverse an escaped string?
Use the JS String Unescaper on this site. It accepts an escaped string with or without surrounding quotes and converts the escape sequences back into the original characters.
Does the escaper handle emoji and other languages?
Yes. With the Unicode option off, those characters are left as readable text. With it on, they become Unicode escapes, including surrogate pairs for characters outside the basic multilingual plane, and the runtime value is identical either way.
Is my text uploaded anywhere?
No. Escaping runs entirely in your browser, and nothing is sent to a server.
Related Tools
These tools handle the reverse operation and the surrounding tasks of working with strings in code.
- JS String Unescaper – convert an escaped JavaScript string back into plain readable text.
- JavaScript Validator – check that code containing your new literal has valid syntax.
- JS Snippet Tester – print a literal and confirm it holds exactly the text you intended.
- JS HTML Entity Tool – escape text for safe placement in HTML, a different kind of escaping for a different context.
- JSON Formatter – format JSON data when your strings live inside a data file rather than code.