Escaping Literal Quotes in Strings 2017

Tell us what’s happening:

Your code so far

var myStr = "I am a, \" "double qouted" string "inside double quotes\"."; // Change this line


Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36 OPR/49.0.2725.47.

Link to the challenge:

You have a couple issues:

  1. Your sentence has an extra comma in it
  2. Your sentence has a typo in “double qouted”
  3. You are not escaping the double quotes.

If I had the following string:

John said “He will be late.”

And I wanted to assign this string to a variable, I would do:

var myString = "John said \"He will be late.\"";

I had to put the escape character \ in front of each " within the string. Notice, I did not use the escape character before the two end double quotes.

Thank you for your help!

Can you explain why this is? You would think that the second double quote would go before the second escape slash

In my example above, if I started with the string that did not contain any double quotes, it would look like:

var myString = "John said He will be late.";

If I want to put a double quotes around the word He while still using double quotes on the ends, then I would have to put a " before the H and a " after the e in the word He, so I would have to use:

var myString = "John said \"He\" will be late.";

If a double quote was put before a slash without a slash before the double quote, then it would not show in the string as a double quote.