Escaping Literal Quotes in Strings Lesson

Can anyone tell me what is the use of it?
Thanks.

Quotes aren’t the only special character, but being able to escape them is a handy thing. For example, tabs or newline u return characters are also not string friendly, unless escaped or in a string literal.

I guess I’m confused. Are you asking what’s the use of being able to insert a quote mark into a string?

No, my question is why do I need to escape a quote?

Sometimes you want a " inside of your string, but if you just put a " inside of a string that is surrounded by "s, then you will actually be closing the string instead of adding a ".

const badString = "I want to add a quote here " but that closed the string";

I think I understand what you mean, it is a way to prolong your string is that a correct way of looking at it?

I don’t know what ‘prolong’ means in this context. The escape character is just a piece of syntax needed to be able to do this.

const goodString = "I want to add a quote here \" correctly";
const badString = "I want to add a quote here " incorrectly";
1 Like

Thanks for the help :slight_smile:

1 Like

But you have to “close it” right?

I mean you have to add another backlash at the end of the word is that correct? for example
const goodString = "I want to add a quote here \" correctly";

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

You don’t need any extra backslashes. You only need to add a backslash for the quote you are ‘escaping’.

const sampleStr = "Alan said, \"Peter is learning JavaScript\".";

so “peter is learning JavaScript” is escaping the main quote, how come it has double backlashes then?

shouldn’t it be like this

const sampleStr = "Alan said, \"Peter is learning JavaScript".";

Within the string, special characters need to be escaped, each time. Might be only one:

// one...
const foo=' Since I\'m wrapping this in single quotes, i must escape that single quote.';

// or more
const bar="I say \"yes,\" you say \"no.\""

Each time a special character is encountered, it should be escaped. When single quotes enclose the string, a single quote is considered special. When double quotes enclose a string, a double quote is special.

1 Like

Thanks, I think i got it, your explanation was clear.

in the first line you have to escape it, to make sure it doesn’t end the string by mistake am I correct?

const foo=' Since I\'m wrapping this in single quotes, i must escape that single quote.';
1 Like

Exactly right. The first un-escaped single quote (in this case) ends the string. If we’d left the one in the middle without the \, it would have ended the string and caused the rest of our intended string to be…an error message.

1 Like

Thanks a lot, you helped me understand this better :slight_smile:

1 Like