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

A bit of confusion here with quotations.

This is for the JavaScript Basics Making Decisions with Conditional Statements (Introducing Conditional Statements) lesson. This is in reference to a true condition.

I'm a little confused right now about using quotations in JS. In the earlier lessons, Dave had said that as long as you are consistent with quotations within your code, it won't matter if you use " vs '. In this part of the video, when I tried to run part of a conditional statement with document.write inside, it wouldn't accept the ', only ". Is there a reason for this?

Here's a snapshot of the workspace. It works fine, I just don't understand why I needed to switch my quotations.

https://w.trhou.se/s8or5uzmel

1 Answer

When you are using quotations for your strings, there will be common times where you need to place the ' in words like there's, or where's. If you was to use ' to surround the whole of a string with these characters used inside that string as well, the JavaScript interpreter would think you are wanting to close the string and then class the rest of the text as an error. That is why it is preferred to use " when stating strings that may contain these characters (').

This does not mean you cannot use the ' when writing strings, you can always place a \ before the quotation mark that is just a character in the string and not meant to end the string. This allows the interpreter to ignore the quotation mark and keep it inside the string without affecting the code.

Hope this makes sense. It is explained very well in the videos, I'd go back and watch the videos again if you are still unsure.

Thanks, Josh. That helps!