Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

‘do ... while’ syntax with a semicolon

Thank you, Dave McFarland for this JavaScript course. I like its pace and your voice and your vocal energy is terrific! I have a question about ‘do while’ syntax. According to the link you provided in teacher’s notes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while, there is a semi-colon ";" at the end of ‘do while’ loop:

Syntax:

 do
      statement
 while (condition);

However, in your video and in your teacher’s notes it is not present:

 do {
      // code for loop goes here
      // it runs AT least one time
 } while ( )

Also, in the review quiz, sometimes “;” IS present at the end:

"Finish this code to create a do...while loop."

 var counter = 0;
 do {
      console.log(counter);
      counter += 1;
 } while  (counter < 10);

And sometimes it is not:

"Finish the code in this loop's condition, so that 'I love JavaScript' is printed to the console 10 times: "

 var x = 0;
 do {
      console.log('I love JavaScript');
      x += 1; 
 } while ( x <=  )

"Given the code below, what will print out to the console?"

 do {
      console.log('Hello');
 } while (false)

I understand that JavaScript is a loosely typed language and it will add a “;” for you. I just found it confusing when there is no consistency in the syntax. Thank you.

2 Answers

Charles Smith
Charles Smith
7,575 Points

This might shed some light.

Basically, syntactically, it's often a style choice.

Thank you for your answer. I came across this article as well prior to posting. Clearly, it says the same:

"// BUT:

do {...} while (...); "

Basically, they also say that a semicolon should be present at the end of a "do while' loop.

I agree with you that probably, it was a style choice. Even invoking strict mode won't give you any errors. But at least we should stick to one style and be consistent :)

Thank you very much.