JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops For Loops

Dave's mistake?

In the "for" loop, Dave adds +=1 to the i variable, but doesnt put semi-colon at the end of it, yet it still runs. Shouldn't he put semi-colons?

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 144,810 Points

Hi Damjan,

While the current convention is to use semicolons, with JavaScript, semicolons are actually not always required. Semicolons are statement separators. In a for loop, yes they would be needed for the first two, because they are on the same line and need to be separated, but the parser will automatically insert a semicolon at the end of the line.

There is quite the debate going on about whether the convention should be pro semicolon or no semicolons. If you want to read up more on this debate, just Google Search "semicolon in JavaScript" for many, many articles and posts.

So, in short, no Dave is not incorrect.

Keep Coding! :) :dizzy:

Marcin Jerwan
Marcin Jerwan
4,628 Points

in the "For Loops" video, at 03:06 mark, top condition is counter<10, and at the bottom example counter<=10 . is this a simple mistake or is it meaningful?