Escaping literal quotes in string help please

Tell us what’s happening:
Describe your issue in detail here.

Your code so far


var myStr = I am \"double quoted\" string inside \"double quotes\".";
console.log(myStr)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36

Challenge: Escaping Literal Quotes in Strings

Link to the challenge:

You have two problems here. First of all, you need a " at the beginning or your string to tell JS that that is a string.

Secondly, you have removed the word “a” from the quote.

When I fix those, your code passes for me.

1 Like

Sir should I put " at the beginning of my string ? i mean I ready have "\ before string so I’m confused on how to put it

You are confusing the quotes that go around the string and the quotes that are part of the string.

For example:

console.log("She called it food but  no one dared eat it.");
// She called it food but  no one dared eat it.

Here we wrap the string in " marks to tell JS that this is a string. Notice that there are no " inside the string. If we want " around “food”, then I could just use a different symbol to surround the quote, like this:

console.log('She called it "food" but  no one dared eat it.');
// She called it "food" but  no one dared eat it.

That works fine because JS can see that the symbol surrounding the quote is different than the symbol inside the quote.

If we wanted to use " for the outside too, then things get hinky…

console.log("She called it "food" but  no one dared eat it.");
// error

JS can’t tell if that second " it encounters is the end of the string or is meant to be displayed in the string. That’s OK, when we have chars that we can’t write in the string, we can use an escape. So, I can do this:

console.log("She called it \"food\" but  no one dared eat it.");
// She called it "food" but  no one dared eat it.

That works fine. When JS gets to that \", it knows that that is not the end of the string but that we want a " inside the string.

Does that makes sense?