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";
Thanks for the help
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 (â).
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.
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.';
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.
Thanks a lot, you helped me understand this better
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.