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(`
{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.
Videos
What Are Tips for Using Template Literals Instead of Escape Characters?
What Are Escape Characters?
How Can Escape Characters Be Combined in Strings?
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(`
{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.
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.