Why strings need escape characters
Quotes mark where a string starts and ends, so a quote inside the text can be mistaken for the end. A backslash (\) placed before a character changes how JavaScript reads it. That backslash is the escape character, and the two characters together are an escape sequence. It lets you put quotes, backslashes and line breaks inside a string.
The simplest way around the quote problem is to pick the other kind of quote for the outside:
console.log("My name's Andy");
console.log('Double quotes "inside single" quotes');
That works until the text contains both kinds. In 'After "Coding 101" we'll solve some problems', the apostrophe in we'll ends the string early and the rest is a SyntaxError. The same happens with double quotes on the outside. Escaping solves it.
Escaping quotes
Put a backslash before the quote that should stay part of the text. JavaScript then reads it as an ordinary character:
console.log('My name\'s Andy');
console.log("Double quotes \"inside double\" quotes");
console.log("After \"Coding 101\" we'll solve some problems");
My name's Andy
Double quotes "inside double" quotes
After "Coding 101" we'll solve some problems
The backslash is not part of the string. In "\"" the string holds one character, a double quote. You only need to escape the kind of quote that surrounds the string: an apostrophe inside double quotes can stay as it is.
Escaping the backslash itself
Since a backslash starts an escape sequence, a literal backslash has to be written as two:
console.log('C:\\Users\\Andy\\Games'); // C:\Users\Andy\Games
console.log('C:\Users\Andy\Games'); // C:UsersAndyGames
In the second line each \U, \A and \G is read as an escape sequence that JavaScript does not define, and an unknown one simply stands for the character itself, so the backslash disappears. Worse, some letters after a backslash have a meaning of their own. A path like C:\Users\Andy\notion contains \n, which is a line break, so a single backslash would split the path in two lines. Doubling every backslash is the fix: \\ is one backslash.
Common escape sequences
| Write | You get |
|---|---|
\' | a single quote |
\" | a double quote |
\\ | one backslash |
\n | a new line |
\t | a tab |
\r | a carriage return |
\u0041 | the character with that code (here A) |
\u{1F600} | a character by its code point, which also covers emoji |
\x41 | the character with that two-digit hexadecimal code (here A) |
New lines with \n
The letter n prints as a normal letter, but \n is a line break. It lets one console.log print several lines:
console.log("Hello world!\nWhat a good day for coding!\n");
console.log("Let's have a blast!");
Hello world!
What a good day for coding!
Let's have a blast!
There is an empty line in the middle: the \n at the end of the first string ends the line, and console.log adds its own line break after every call. A \n counts as one character, so "a\nb".length is 3, while "a\\nb" (a backslash followed by an n) has length 4.
Backticks and raw strings
Template literals, which use backticks, can hold both kinds of quote without any escape, and they can run over several lines:
console.log(`She said "it's fine"`);
console.log(`line1
line2`);
A backtick inside a template literal is escaped like the others: \`. If you want backslashes to stay exactly as typed, tag the literal with String.raw: String.raw`C:\Users\Andy\notion` gives C:\Users\Andy\notion without doubling anything.
The exercise, step by step
- A message with quotes. Print
I'll study "Coding 101" on AlgoCademy. Wrap the text in double quotes. The apostrophe inI'llneeds nothing, and each inner double quote gets a backslash. - Two lines from one call. Print
I will learn thisandNo matter whaton separate lines using a singleconsole.log. Put\nbetween them inside one string. - A file path. Print
C:\Users\Andy\notion. Each of the three backslashes is written as\\. This is the case where a single backslash would be dangerous, because\nin\notionwould be a line break.
console.log("I'll study \"Coding 101\" on AlgoCademy");
console.log("I will learn this\nNo matter what");
console.log("C:\\Users\\Andy\\notion");
I'll study "Coding 101" on AlgoCademy
I will learn this
No matter what
C:\Users\Andy\notion
Common mistakes
- Forgetting to double backslashes in Windows paths. Backslashes silently vanish or turn into tabs and line breaks. Doubling them, or using forward slashes (
"C:/Users/Andy"), avoids the problem. - Escaping the wrong quote. In a double-quoted string,
\'works, but it is unnecessary. The quote that matches the outside is the one that must be escaped. - Typing the escape as text. In
"a\\nb"there is no line break: it prints a backslash and ann.
Key takeaways
- A backslash before a character changes its meaning: it turns quotes into plain text and letters such as
nandtinto line breaks and tabs. - Escape the quote that surrounds the string, or switch to the other kind of quote or to backticks.
- Write
\\for a literal backslash. \nis one character, a line break, andconsole.logadds another after the text.
