JavaScript Escape Sequences

Home » JavaScript Tutorial » JavaScript Introduction » 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.

Escape sequences fall into two main categories: sequences for special characters (like quotes and backslashes) and sequences for formatting (like newlines and tabs).

These are essential for using the quote character that defines the string or for including a literal backslash.

SequenceEscape DescriptionExampleOutput in the Console
\’Single Quote Escapeconst a = 'It\'s a beautiful day.';It’s a beautiful day.
\”Double Quote Escapconst b = "He said, \"Hello!\"";He said, “Hello!”
\Backslash Escapeconst c = "C:\\Users\\Data";C:\Users\Data
\nNew Lineconsole.log("Line 1\nLine 2");Line 1 (on one line)
Line 2 (on the next line)
\tTabconsole.log("Name:\tAlex");Name: Alex (includes a tab space)

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! 🎉
JavaScript

To see more unicode characters to add to your JavaScript, see https://www.rapidtables.com/code/text/unicode-characters.html.

Objects like Math and Date are pre-built by the JavaScript engine to perform complex actions. They illustrate that the Object type isn’t just for storing custom data but also for providing utility methods to the language.