JavaScript String Escape Characters?
Codecupdev
Posted on July 11, 2022
Introduction
In JavaScript when we are working with strings sometimes we want to break up the string in some way. We have some escape characters which we can use to help us with this challenge.
- \’ Single quote
- \” Double quote
- \n Newline
- \ Backslash
Quotes
When you are working with strings if you want to use a quote within the string you cannot use the same quote marks for the quote inside the string as the ones you wrap the string in. Let’s look at some examples.
const quote = "A famous quote is "I think, therefore I am" "
//Returns ---> Uncaught SyntaxError: Unexpected identifier
const secondQuote = 'A famous quote is 'I think, therefore I am' ';
//Returns ---> Uncaught SyntaxError: Unexpected identifier
In the above example we start by declaring a variable with the identifier quote and assign to this a string enclosed in double quote marks which contains a quote which is also enclosed in double quote marks. Next we create a second variable called secondQuote which repeats the same steps again but this time it uses single quote marks. Both of these return a syntax error. One way around this problem would be to use different quote marks for the quote as is shown in the example below.
const quote = "A famous quote is 'I think, therefore I am' "
const secondQuote = 'A famous quote is "I think, therefore I am" ';
In the above example the string stored inside the variable quote uses single quote marks for the quote and double quote marks for the string. The string for the secondQuote variable uses single quote marks for the string and double quote marks for the quote. Both of these work. Alternatively we could use the escape characters for these as shown in the example below.
const quote = "A famous quote is \"I think, therefore I am\" "
const secondQuote = 'A famous quote is \'I think, therefore I am\' ';
In the above example we use a backslash and the double quote mark at the start and end of the string stored inside the quote variable. We repeat the same steps but we use a backslash and a quote mark for the string stored inside the secondQuote variable.
Another time this is useful is if you wish to use apostrophes within the string.
const phrase = 'You\'ve had eight cookies';
//Returns ---> "You've had eight cookies"
Newline
When you want to break up your string over multiple lines, one option available is to use the newline character. We place this where we want the new line to appear. Let’s look at an example.
"Monday, Tuesday, Wednesday";
//Returns ---> 'Monday, Tuesday, Wednesday'
"Monday,\nTuesday,\nWednesday";
//Returns --->
"Monday,
Tuesday,
Wednesday"
Backslash
If you want to use a backslash within your string you need to use two back slash characters. Given we use the backslash when we use an escape character we can’t just use a single backslash in a string. Let’s look at some examples.
"Hello \";
//Returns ---> Uncaught SyntaxError: Invalid or unexpected token
"Hello \\";
//Returns ---> 'Hello \'
Posted on July 11, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.