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 trialCarlos Ramirez
5,084 PointsWhy using i += 1 instead of i++ in the third part of for declaration?
Why using i += 1 instead of i++ in the third part of for declaration. I have seen many videos which uses i += 1. Why?
2 Answers
Greg Kaleka
39,021 PointsHey Carlos,
These two are exactly the same. It's just two different ways of writing the same thing. i++
is just a shortcut for i += 1
, which itself is a shortcut for i = i + 1
. These all do the same thing, and it's just a question of how explicit you want to be.
Cheers
-Greg
dylan jasper
1,928 PointsI watched a video from Douglas Crockford (guy who wrote JavaScript: The Good Parts) who said that he never uses the prefix or postfix ++ operator as some people donβt understand the difference and it can lead to mistakes. He just sticks with x += 1 style.
Carlos Ramirez
5,084 PointsCarlos Ramirez
5,084 PointsThank you. My question is why teachers in treehouse decide to use i += 1 instead of i++ (which is more commonly used at least in Java). You know i += 1 is harder to write and also is more easy to make mistakes. I saw a question months ago which someone following the courses wrote i =+ 1. What I mean is that must be a very good reason for using i += 1, I want to know which is.
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsThere's not really a reason other than preference.
If you want a reason for why someone might have a preference for += 1 over ++, a recent change to Swift (for iOS programming) gives a great explanation.
Swift did away with the ++ operator altogether. Here are the reasons Chris Latner, the creator of Swift, cited (not all of these apply to Javascript, but many do):
Here's a Stack Overflow thread specific to Javascript that has good arguments on both sides of the "debate".
Steven Parker
231,275 PointsSteven Parker
231,275 PointsNote that "
i++
", "i += 1
", and "i = i + 1
" are all the same when used as stand-alone statements. But when used as an expression, "i++
" returns the value before incrementing, while the others return the final value. A fourth option is "++i
" which also returns the final value.Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsGood point Steven - that distinction is really the reason for item 6 on Chris's list. The fact that you can do that is also the reason for # 5. If you're taking advantage of the difference between i++ and ++i, you're probably getting too cute.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsI don't think I'd use the term "cute", but I agree that making use of the difference between the pre-increment and post-increment operators is definitely an advanced topic and not recommended for beginning programming. I only brought it up as a point of information.
For most students, I recommend sticking to the addition assignment operator ("
+=
") for consistency and readability.Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsMy mistake
Sure - there are arguments to be made for the operators. They exist, after all! Absolutely worth bringing up.