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

For Loop Syntax

In the for loop, what is the difference between using i++ vs i+=?

2 Answers

i++ is equivalent to i += 1. Increments i by 1

If you wanted to increment by a number other than 1 each time through the loop, you'd have to use the += version.

Let me know if this helps here

The ++ operator is a shorthand that adds 1 to whatever variable you use it on. So i++ is equivalent to saying i = i + 1.

The += operator is a shorthand that allows you to add whatever number you want to a variable. i += 1 would be equivalent to i = i + 1 (so same as i++) but you could also do i += 10 for example which would be equivalent to i = i + 10.

So ++ is useful if you are only adding 1 to a variable, and += is useful if you want to add some number other than 1 to a variable. But ultimately they are both simply shorthands for adding a number to another number.