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 the exact string
It's working!using a single-quoted string (escape the apostrophe) - Log the exact string
She said "Hi"using a double-quoted string (escape the inner quotes) - In a single string, log
HelloandWorldon separate lines (use a newline escape) - Log the exact tab-separated headers:
Name:then tab thenAge:then tab thenCity:
Completion criteria
Your program output should include all four of these (exact text and whitespace):
It's working!She said "Hi"HelloandWorldseparated by a newline in one logName:\tAge:\tCity:(tabs between the labels)
π° Use backslash escape sequences for quotes, newlines, and tabs. The table
above is your reference.