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. Mastering these sequences gives you precise control over how text is displayed and formatted.
Common Escape Sequences
Escape sequences fall into two main categories: sequences for special characters (like quotes and backslashes) and sequences for formatting (like newlines and tabs).
Quotes, Backslashes, and Formatting
These are essential for using the quote character that defines the string or for including a literal backslash.
| Sequence | Escape Description | Example | Output in the Console |
| \’ | Single Quote Escape | const a = 'It\'s a beautiful day.'; | It’s a beautiful day. |
| \” | Double Quote Escap | const b = "He said, \"Hello!\""; | He said, “Hello!” |
| \ | Backslash Escape | const c = "C:\\Users\\Data"; | C:\Users\Data |
| \n | New Line | console.log("Line 1\nLine 2"); | Line 1 (on one line) Line 2 (on the next line) |
| \t | Tab | console.log("Name:\tAlex"); | Name: Alex (includes a tab space) |
Hexadecimal and Unicode Characters
Hexadecimal and unicode character sequences allow you to represent any character, including emojis, special symbols, and characters from international alphabets, using their numerical code points. Similar to using Hexidecimal “x\…” and Unicode characters “\u…” in HTML, they can be used to add characters in JavaScript.
const helloText = '\x48\x45\x4C\x4C\x4F';
console.log(helloText);
// Output: HELLO
const euroSymbol = '\u20AC';
console.log(`The currency is: ${euroSymbol}`);
// Output: The currency is: €
const emoji = '\u{1F389}'; // Confetti Popper emoji
console.log(`Let's celebrate! ${emoji}`);
// Output: Let's celebrate! 🎉JavaScriptTo see more unicode characters to add to your JavaScript, see https://www.rapidtables.com/code/text/unicode-characters.html.




