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 (Retired) Storing and Tracking Information with Variables Capturing Visitor Input and Writing It to the Page

mustafa attaie
mustafa attaie
8,068 Points

when writing a document.write() do you need to end it with a ;

I was finishing up a quiz when i accidentally forgot to add a ; at the end of my document.write() it was on a complete code of it's own. I already entered submit my answer when i realized i forgot a ; at end. It passed me, now I'm confused, is it correct to write certain keywords without a ; or did i just get a lucky break?

3 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

JavaScript has built in functions to try and insert semicolons when they are omitted by the programmer. However, it is best practice to end your statements with a semicolon. The automatic insertion can lead to some ambiguous code otherwise. Here's a link to the MDN where it talks about Automatic Semicolon Insertion https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion

A semi-colon is used to end a statement and imply that your code continues after that. In some cases, a line break can successfully end a statement. However, not using a semi-colon can cause issues depending on the code that follows it and it is best to use a semi-colon. If the only code you have is document.write("something"), that will work with or without a semi-colon because nothing comes after it.

I found the following example on Quora that may help you: https://www.quora.com/Why-should-I-use-semicolons-in-javascript

Eric White
Eric White
10,539 Points

Yes you are correct, To end a statement in Javascript you would use a semi-colon. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Expression_closures