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 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.