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

Code challenge: Create a for loop. Not fully understanding the syntax.

Hello, could someone please explain why "i += 1" does not require a semi-colon in this for loop? I understand the logic, but found adding a semi-colon caused a syntax-error.

for ( var i = 4; i <= 156; i += 1 ){
console.log(i) }

Thanks, Dan

(Edited by BothXP to format the example)

3 Answers

Hi Dan,

The displaying of code examples can get a bit messed up if you don't use the Markdown formatting, so i added it in around your code example just to make it a bit clearer.

I really liked the question because I'd never really thought about it like that.

The MDN has a nice explanation of for loops: "The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement or a set of statements executed in the loop."

But I think that the best answer that we can give you is that it's just the way the syntax of a for loop is designed. I just think of the ';' as separating the 3 expressions rather than being the terminator of each one.

Hey thanks for your response it was very helpful.

I've always thought of ';' as being a terminator as you say and the lack of it's use on this occasion was just different to what had been taught up to this point.

Also thanks for adding the Markdown formatting (how do you do that?).

The MDN explanation of loops is really good. I should use MDN more but as a newbie to JavaScript, find it a bit overwhelming at times!

Cheers.

Hello. A FOR LOOP has 3 main parts. The starting point, the condition and incrementation. Look at the code below

for(var i = 0 ; i < 10 ; i++){
//some code here
}

Here i created a for loop. First i write the word 'for' and after that i added some brackets. Inside the brackets, you need to specify the three parts i mentioned.

  1. First we created a counter variable called 'i' and set it's value to zero. This will tell the loop that we want the loop to start when 'i' is equal to zero
for(var i = 0){
some code here//
}
  1. Second we set a condition for our for loop. Here we specified that we want to run the loop over and over again WHILE 'i' is less than 10.
for(var i = 0 ; i < 10){
some code here//
}
  1. Finally we created the incrementation. This will add a certain value to the variable 'i' each time the loop runs. here it will add 1 to 'i' each time the loop runs so that when 'i' is not less than 10 ...the loop will end.
for(var i = 0 ; i < 10 ; i++){
//some code here
}

This is the best i can explain...hope i did a good job...please mark my answer as the best

Thanks for taking the time to respond, like everyone else's response I've taken plenty from your explanation. Thanks!

Dan, The reason that you don't need a " ; " at the end of the for is because it is redundant. The " ) " marks the end of the loop syntax.

the reason you are getting a syntax error is because the for loop you posted is incorrect syntax.

for ( var i = 4; i console.log(i) }

I don't know if this is the entire loop but the correct syntax for a for loop is as follows:

for ([initialization]; [condition]; [final-expression]) {

statement

}

so from the example you have posted your loop does not have a condition nor a final-expression, just initialization and statement.

I hope this helps.

Thank you for your answer very helpful - for some reason it hadn't posted my full code I did try twice! for ( var i = 4; i <= 156; i += 1 ){console.log(i) } this is what I actually had . Thanks again.