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

Why does JavaScript allow you to put the <p>....[adjective]...[verb]....[noun].</p> underneath the //2.?

It seems strange. In a block of script we have a loose paragraph? [Did we ever go over double slash comment? I must have missed it.]

5 Answers

Cameron Childres
Cameron Childres
11,818 Points

Ah I see what you're saying, that's a weird one!

It looks like they're just using that space to think through their text -- it's not something you would ever want to leave in the final code and it's not a normal way of 'commenting'. I would not recommend getting in the habit of leaving loose strings in your code.

The reason that text doesn't appear to do anything is because it's surrounded by quotation marks (making it a string) and nothing is done with the string. When that line is read the string is returned but isn't used for anything so it just sort of disappears in the background. It would be similar to having a random unused number on its own line in your code.

If a string is on its own line it is returned silently and forgotten, the rest of the code can still run:

console.log("First");
"Second";
console.log("Third");
// First
// Third

If it is not in quotes then an attempt will be made at interpreting it and you'll get an error, preventing the following code from executing:

console.log("First");
Second;
console.log("Third");
// First
// Uncaught ReferenceError: Second is not defined
Cameron Childres
Cameron Childres
11,818 Points

Hi Jason,

I'm not sure which course or code you're referring to but the double slash is indeed a single line comment. If a line starts with // then everything after the slashes that is on the same line is ignored. You'll also see multi-line comments which will start with /* and end with */.

Examples:

// This is a single line comment. Text on this line will be ignored.

/*
This is a multi-line comment.
All text between the start and end,
no matter how many lines,
will be ignored.
*/

You can find more information here: https://www.w3schools.com/js/js_comments.asp

I hope this helps! Let me know if you have any questions.

In the video it seemed as if JavasScript allowed non-commented text to be present and still ran.

Cameron Childres
Cameron Childres
11,818 Points

Would you mind sharing a link to the video you're talking about?

Line 5 in the video's workspace and it starts being typed at 0:27 time. It is strange to me that you can run his later code for (//1 ...).

<a href="https://teamtreehouse.com/library/variables-and-strings-challenge-solution"> Variables and Strings Challenge Solution</a>

I don't know why all my questions come up "JavaScript" -- it is strange; I just try to use the site -- not trying to figure it out. I also can't say your answer is the best answer even though the site e-mailed me asking me to do so... my guess is you're a moderator. I can make my questions best answers, but that is really weird.