🌐
W3Schools
w3schools.com › js › js_strings.asp
JavaScript Strings
February 23, 2026 - Templates are strings enclosed ... be chopped to "We are the so-called ". To solve this problem, you can use an backslash escape character....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › escape
escape() - JavaScript - MDN Web Docs - Mozilla
You can replace %XX with \xXX and %uXXXX with \uXXXX to get a string containing actual string-literal escape sequences.
People also ask

What Are Escape Characters?
Escape characters are special sequences used in strings to achieve specific formatting or include characters that are otherwise reserved or difficult to type. By starting with a backslash (\), these sequences signal JavaScript to interpret the next character differently. In simple terms, escape characters "break the rules" of normal string syntax. For instance, using a single quote inside a string surrounded by single quotes normally causes an error. However, using an escape character like \' resolves this issue. They are essential for clean, error-free code when dealing with complex strings.
🌐
d-libro.com
d-libro.com › javascript coding with ai › escape characters
Mastering Escape Characters in JavaScript: A Complete Guide - Topic
What Are Commonly Used Escape Characters?
Escape characters in JavaScript serve specific purposes, such as formatting strings, embedding special characters, and creating readable code. Commonly used escape characters include: New Line (\n) and Tab (\t): \n adds a new line, splitting text into multiple lines, while \t inserts a tab space, useful for alignment and structured output. Backslash (\\) and Quotes (\', \"): \\ represents a literal backslash, crucial for file paths or escaping other characters. \' and \" allow single or double quotes to be included within strings without causing syntax errors. Special Characters (\u U
🌐
d-libro.com
d-libro.com › javascript coding with ai › escape characters
Mastering Escape Characters in JavaScript: A Complete Guide - Topic
What Are Tips for Using Template Literals Instead of Escape Characters?
Template literals, enclosed by backticks (`), often eliminate the need for escape characters. They allow for multi-line strings and embedded expressions, making code more readable and maintainable. Template literals will be explained in more detail later in this chapter.
🌐
d-libro.com
d-libro.com › javascript coding with ai › escape characters
Mastering Escape Characters in JavaScript: A Complete Guide - Topic
🌐
Mathias Byens
mathiasbynens.be › notes › javascript-escapes
JavaScript character escape sequences · Mathias Bynens
December 21, 2011 - The \ followed by a new line is not a character escape sequence, but a LineContinuation. The new line doesn’t become part of the string. This is simply a way to spread a string over multiple lines (for easier code editing, for example), without the string actually including any new line characters. I suppose you could think of \ followed by a new line as an escape sequence for the empty string.
🌐
Rip Tutorial
riptutorial.com › escape sequence types
JavaScript Tutorial => Escape sequence types
Characters with codes between 0 and 255 can be represented with an escape sequence where \x is followed by the 2-digit hexadecimal character code.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Regular_expressions › Character_escape
Character escape: \n, \u{...} - JavaScript - MDN Web Docs
2 weeks ago - In Unicode-unaware mode, escape sequences within character classes of the form \cX where X is a number or _ are decoded in the same way as those with ASCII letters: \c0 is the same as \cP when taken modulo 32. In addition, if the form \cX is encountered anywhere where X is not one of the recognized characters, then the backslash is treated as a literal character.
🌐
W3Newbie
w3newbie.com › home › javascript tutorial › javascript introduction › javascript escape sequences
JavaScript Escape Sequences - W3Newbie
November 29, 2025 - JavaScript Escape Sequences In JavaScript, escape sequences are combinations of a backslash () followed by one or more characters, used inside string literals to represent special characters that can’t be typed directly or would otherwise break the string’s syntax.
🌐
CodeBasics
code-basics.com › programming › javascript course › escape sequences
Escape sequences | JavaScript | CodeBasics
[JavaScript] — Escape sequences — Imagine you want to print a dialogue between the Mother of Dragons and her child: ``` - Are you hungry? - Aaaarrrgh! ``` If you print a string with this text: ```javascript consol...
🌐
freeCodeCamp
forum.freecodecamp.org › contributors
Learn Basic JavaScript: Escape Sequences in Strings - Contributors - The freeCodeCamp Forum
August 29, 2020 - Hi. In the page https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings you give the ‘output’ of \b as ‘word boundary’. Shouldn’t this be ‘backspace’? It …
Find elsewhere
🌐
D-libro
d-libro.com › javascript coding with ai › escape characters
Mastering Escape Characters in JavaScript: A Complete Guide - Topic
January 13, 2025 - Building Web Applications in JavaScript ... Escape characters are special sequences used in strings to achieve specific formatting or include characters that are otherwise reserved or difficult to type.
🌐
Fridoverweij
library.fridoverweij.com › docs › jstutorial › unicode_and_escape_sequences.html
JavaScript Tutorial – Unicode and escape sequences
May 7, 2025 - Escape sequence: \r\n. A "next ... are defined as listed above, various programming languages treat them in various ways. JavaScript treats \n (or \u{000A}) as a newline instead of a line feed....
🌐
Quackit
quackit.com › javascript › tutorial › javascript_escape_characters.cfm
JavaScript Escape Characters
When working with these characters, ... usually because the browser will interpret it differently to what you intended. In JavaScript, the backslash (\) is an escape character....
🌐
W3Schools
w3schools.com › jsref › jsref_escape.asp
JavaScript escape() Method
The escape() function is deprecated. Use encodeURI() or encodeURIComponent() instead. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report ...
🌐
TutorialsPoint
tutorialspoint.com › escape-characters-in-javascript
Escape characters in JavaScript
<!DOCTYPE html> <html> <head> <title>Escape Characters Demo</title> </head> <body> <script> // Using escaped quotes let singleQuoted = 'He said, "I'm learning JavaScript!"'; let doubleQuoted = "She replied, "That's great!""; document.getElementById('output').innerHTML = '<p>' + singleQuoted + '</p>' + '<p>' + doubleQuoted + '</p>'; </script> </body> </html>
🌐
DEV Community
dev.to › manikbajaj › character-literals-in-javascript-a-few-unused-ones-as-well-21g9
Escape Sequence in JavaScript - A Few Unused Ones as Well - DEV Community
February 21, 2021 - Character literals help you format your strings in JavaScript. We will look at simple escape c... Tagged with javascript, node.
Top answer
1 of 3
147

If your goal is to create a string using a string literal like this:

str = "Hello\nWorld";

and output what it contains in string literal form (a fairly specific use case), you can use JSON.stringify:

console.log(JSON.stringify(str)); // ""Hello\nWorld""

const str = "Hello\nWorld";
const json = JSON.stringify(str);
console.log(json); // ""Hello\nWorld""
for (let i = 0; i < json.length; ++i) {
    console.log(`${i}: ${json.charAt(i)} (0x${json.charCodeAt(i).toString(16).toUpperCase().padStart(4, "0")})`);
}
.as-console-wrapper {
    max-height: 100% !important;
}

This is a fairly unusual thing to want to do, though helpful sometimes for debugging in awkward situations where, for whatever reason, you can't use a debugger directly.

console.log adds the outer quotes (at least in Chrome's implementation), but the content within them is a string literal (yes, that's somewhat confusing).

JSON.stringify takes what you give it (in this case, a string) and returns a string containing valid JSON for that value. So for the above, it returns an opening quote ("), the word Hello, a backslash (\), the letter n, the word World, and the closing quote ("). The linefeed in the string is escaped in the output as a \ and an n because that's how you encode a linefeed in JSON. Other escape sequences are similarly encoded.


To be clear, if you just want to create a string with H, e, l, l, o, \, n, W, o, r, l, d in it, you don't need JSON.stringify for that. Just escape the backslash so it's not treated as an escape character: "Hello\\nWorld"

console.log("Hello\\nWorld");

The JSON.stringify thing is just for situations where you want to see a string-literal-like output for a string with things like newlines in it, usually for debugging.

2 of 3
30

JavaScript uses the \ (backslash) as an escape characters for:

  • \' single quote
  • \" double quote
  • \ backslash
  • \n new line
  • \r carriage return
  • \t tab
  • \b backspace
  • \f form feed
  • \v vertical tab (IE < 9 treats '\v' as 'v' instead of a vertical tab ('\x0B'). If cross-browser compatibility is a concern, use \x0B instead of \v.)
  • \0 null character (U+0000 NULL) (only if the next character is not a decimal digit; else it’s an octal escape sequence)

Note that the \v and \0 escapes are not allowed in JSON strings.

🌐
JavaScript in Plain English
javascript.plainenglish.io › chapter-23-mastering-escape-sequences-in-javascript-a-beginners-guide-to-special-characters-7b17c40e1b9a
Chapter 23:Mastering Escape Sequences in JavaScript: A Beginner’s Guide to Special Characters | by Aryan kumar | JavaScript in Plain English
October 15, 2024 - Chapter 23:Mastering Escape Sequences in JavaScript: A Beginner’s Guide to Special Characters If you’ve ever wondered how to add special characters like a new line or tab space in your JavaScript …
🌐
CodeSignal
codesignal.com › learn › courses › string-manipulation-for-js-beginners › lessons › unleashing-javascript-a-deep-dive-into-escape-characters-and-special-characters
A Deep Dive into Escape Characters and Special Characters
In this instance, the escape character (\) inserts double quotes into the string. Without it, the JavaScript compiler would throw an error. Other frequently used escape sequences include \t (tab), \n (new line), and \\ (backslash).
🌐
freeCodeCamp
freecodecamp.org › news › how-to-escape-strings-in-javascript
How to Escape a String in JavaScript – JS Escaping Example
November 7, 2024 - In JavaScript, you can use the opposite string syntax ' or " to escape a string.
🌐
Quora
quora.com › What-are-the-escape-characters-in-JavaScript
What are the escape characters in JavaScript? - Quora
The unescape() function in JavaScript takes a string as a parameter and use to decode that string encoded by the escape() function. The hexadecimal sequence in the string is replaced by the characters they represent when decoded via unescape(). ... ...