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

Adrian Dzienisik
seal-mask
.a{fill-rule:evenodd;}techdegree
Adrian Dzienisik
Full Stack JavaScript Techdegree Student 5,780 Points

"do...while" syntax semicolon

function getRandomNumber(upper) { return Math.floor( Math.random() * upper ) + 1; }

var randomNumber = getRandomNumber(10); var guess;

do { guess = parseInt(prompt('Guess the number: ')); } while ( guess !== randomNumber );

document.write('Congrats! the number was ' + randomNumber);

Hello community,

So, this is a short version of the challenge just to test one thing. For what I've read adding a semicolon after the "do-while" is often a style choice, and JavaScript is a loosely typed language. However, in my case with this code, if I donΒ΄t add the semicolon after the while condition, the result is an endless loop and the browser of course crashes.

So it looks like is not a style choice after all, or it depends on the browser version or something like that?, or am I missing something here?

Thanks very much!

2 Answers

Steven Parker
Steven Parker
231,275 Points

Semicolons are optional at the end of a line unless "strict mode" is being used.

Out of curiosity, I tested this code (using Chrome) both with and without the semicolon and saw no difference in operation.