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 JavaScript Basics Hello, JavaScript! Debug JavaScript in the Console

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

Why 1st command no working when my 2nd command have some typo

I typed the 2nd command as it have some typo but as it should at least run the first one, but it didn't, once I fixed the problem now both are working? what can be the reason as Guil explained the 1st command runs then the 2nd.

Secondly, In the previous video Guil said, that when we write document.write() everything gets replaced by the message but over here the <h1> Tag gets added to the current page why is that?

1 Answer

The reason the missing closing paren in the console.log line causes the previous line to not run is that the in-browser implementation of JavaScript has to parse or compile the code first (I don't know this stuff well enough to know which is the correct terminology), before it can run the individual lines of code and this line of code cause the compiler to break, so it never gets to the point of running the individual lines. Note that this would happen whether or not the console.log error was on the 2nd line, or the last line.

For the 2nd question, Guil didn't say that document.write replaces everything. He said that when we use document.write from from the console, it overwrites everything. The reason for that is that the document is closed, and when you do a document.write from the console, it opens the document, which wipes everything out, then writes to the document. When you are writing to it with the javascript the way that we are doing, the document is still open, so it performs the write operation on the document when the script is loaded.