Escaping Strings
Escaping Strings
π¨βπΌ Sometimes you need to include special characters inside your strings. What
happens if you want to include a quote character inside a string that's wrapped
in quotes?
If you use single quotes, you need to escape single quotes inside the string
using a backslash (
\):console.log('It\'s great') // Prints: It's great
And if you use double quotes, you need to escape double quotes inside the string:
console.log("He said \"Hello\"") // Prints: He said "Hello"
Special Characters
The backslash also lets you include other special characters that you can't
easily type:
| Escape Sequence | Character |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\' | Single quote |
\" | Double quote |
console.log('Line 1\nLine 2') // Prints on two lines
console.log('Name:\tKody') // Prints with a tab between
π¨ Open
and complete the following tasks:
- Log a string using single quotes that contains an apostrophe (like "It's working!")
- Log a string using double quotes that contains a quoted phrase (like: She said "Hi")
- Log "Hello" and "World" on separate lines using
\nin a single string - Log tab-separated column headers: "Name:" "Age:" "City:" using
\t
π° Use backslash escape sequences for quotes, newlines, and tabs. The table
above is your reference.


