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 Array Iteration Methods Array Iteration `for` vs forEach()

Why 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
Greg Kaleka
39,021 Points

Hey 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 :beers:

-Greg

Thank 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
Greg Kaleka
39,021 Points

There'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):

  1. These operators increase the burden to learn Swift as a first programming language - or any other case where you don't already know these operators from a different language.
  2. Their expressive advantage is minimal - x++ is not much shorter than x += 1.
  3. Swift already deviates from C in that the =, += and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.
  4. Swift has powerful features that eliminate many of the common reasons you'd use ++i in a C-style for loop in other languages, so these are relatively infrequently used in well-written Swift code. These features include the for-in loop, ranges, enumerate, map, etc.
  5. Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage "overly tricky" code which may be cute, but difficult to understand.
  6. While Swift has well defined order of evaluation, any code that depended on it (like foo(++a, a++)) would be undesirable even if it was well-defined.
  7. These operators are applicable to relatively few types: integer and floating point scalars, and iterator-like concepts. They do not apply to complex numbers, matrices, etc.

Finally, these fail the metric of "if we didn't already have these, would we add them to Swift 3?"

Here's a Stack Overflow thread specific to Javascript that has good arguments on both sides of the "debate".

Steven Parker
Steven Parker
231,248 Points

:warning: Note 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
Greg Kaleka
39,021 Points

Good 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
Steven Parker
231,248 Points

I 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
Greg Kaleka
39,021 Points

My mistake :blush:

Sure - there are arguments to be made for the operators. They exist, after all! Absolutely worth bringing up.

dylan jasper
dylan jasper
1,928 Points

I 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.