What escaping is
Some characters have a job in the language's syntax. A quote tells the compiler where a string starts and ends. A backslash \ tells it that the next character is special. Inside a string, you often need a character without its job, or a character that has no keyboard key of its own, such as a new line. Escaping does this: you write a backslash followed by a character, and the pair, called an escape sequence, stands for one character in the string.
Quotes inside a string
Switching the outer quotes solves the problem when the text has only one kind of quote. When the text has both, as in After "Coding 101" we'll solve some problems, neither outer quote works. A backslash before a quote makes it plain text:
console.log('My name\'s Andy');
console.log("Double quotes \"inside double\" quotes");
console.log("After \"Coding 101\" we'll solve some problems");
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
After "Coding 101" we'll solve some problems
You only need to escape the quote kind that matches the outer quotes. In the last line the double quotes are left alone, because the string is wrapped in single quotes.
Printing a backslash
Since a backslash starts an escape sequence, a lone backslash in a string does not stay in the string. This line looks harmless:
console.log('C:\Users\Andy\Games');
C:UsersAndyGames
In each pair, the backslash is dropped and the letter after it stays. To keep a backslash, escape it with another one:
console.log('C:\\Users\\Andy\\Games');
C:\Users\Andy\Games
Here \\ is one backslash character, so the string holds 3 backslashes even though the code shows 6.
The new line and other sequences
Escaping also works in the other direction: a backslash before an ordinary letter can give it a special meaning. The most common case is \n, a new line:
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!
The empty line is the second new line: one from the \n at the end of the first string, and one that console.log adds after every message. The string is still one value; the new line is a single character inside it.
A few other sequences are worth knowing: \t is a tab, \\ is a backslash, \' and \" are the two quote characters, and \u0041 is a character given by its Unicode code, which here is A.
The exercise
The lesson has three steps, each printing one message with console.log.
- Print
I'll study "Coding 101" on AlgoCademy. Wrap the text in single quotes and escape the apostrophe inI'llwith\'. The double quotes need no escape inside single quotes. - Print two lines with a single
console.log. Put\nbetweenI will learn thisandNo matter what. - Print
C:\Users\Andy\notion. Double every backslash. This step shows why it matters: an unescaped\nin\notionwould be read as a new line, not as the two characters in the path.
console.log('I\'ll study "Coding 101" on AlgoCademy');
console.log("I will learn this\nNo matter what");
console.log("C:\\Users\\Andy\\notion");
The output is:
I'll study "Coding 101" on AlgoCademy
I will learn this
No matter what
C:\Users\Andy\notion
Gotchas
- The backslash is part of the code, not of the string. The string
"a\"b"has three characters:a,"andb. - The escaped backslash is easy to forget in paths. Unknown sequences like
\Usilently lose the backslash, so you get no error, only wrong output. A sequence like\nor\tin a path changes the output more visibly. - Escaping a quote that doesn't need it is harmless.
"it\'s"and"it's"are the same string. Prefer the simpler one. - Escapes work inside quotes. A backslash outside a string is not an escape sequence, and the compiler reports it as an error.
Key takeaways
- A backslash plus a character is an escape sequence that stands for one character.
\'and\"put a quote in a string without ending it.\\is how you write a real backslash.\nis a new line and\tis a tab.