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 a string using single quotes that contains an apostrophe (like "It's working!")
  2. Log a string using double quotes that contains a quoted phrase (like: She said "Hi")
  3. Log "Hello" and "World" on separate lines using \n in a single string
  4. 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.

Please set the playground first

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