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 SequenceCharacter
\nNewline
\tTab
\\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
index.ts
and complete the following tasks:
  1. Log the exact string It's working! using a single-quoted string (escape the apostrophe)
  2. Log the exact string She said "Hi" using a double-quoted string (escape the inner quotes)
  3. In a single string, log Hello and World on separate lines (use a newline escape)
  4. Log the exact tab-separated headers: Name: then tab then Age: then tab then City:

Completion criteria

Your program output should include all four of these (exact text and whitespace):
  • It's working!
  • She said "Hi"
  • Hello and World separated by a newline in one log
  • Name:\tAge:\tCity: (tabs between the labels)
πŸ’° Use backslash escape sequences for quotes, newlines, and tabs. The table above is your reference.

Please set the playground first

Loading "Escaping Strings"
Loading "Escaping Strings"
Login to get access to the exclusive discord channel.
Loading Discord Posts