Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

\" question

Why does alert("The string \"" + completeName + "\" is " + characterCount + " characters long."); works but alert("The string "\" + completeName + \" + " is " + characterCount + " characters long."); doesn't work?

Show your whole code. It's hard to guess.

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```

1 Answer

\" is so called escape sequence used in strings. It removes the special meaning of " so that it can be used inside a string. If this is confusing, you can use different quotation marks (single or double) like this:

"this isn't a problem" or the other way around: 'he said "happy coding!"' but eventually you are going to need this: 'He said "don\'t mess with the quotes"'

alert("The string \"" + completeName + "\" is " + characterCount + " characters long.");
alert("The string "\" + completeName + \" + " is " + characterCount + " characters long.");
//lets break the second line's string in parts:
"The string "\" //this is missing closing quote
+ completeName //this is ok
+ \" //here you have forgotten the quotes around the string


alert("The string "\"" + completeName + "\"" + " is " + characterCount + " characters long."); //this should work

sorry if there are still errors, did not test my code. Anyhow, the problem is that you had not enclosed all the strings in double quotes properly.