Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

2 Answers

Hi

i++ is a postfix, it will first use (or return) the original value of i and then increment the value of i afterwards. In this example j will be set to the starting value of i, and then i will increment by 1.

> i = 10;
10
> j = i++;
10
> i
11
> j
10

++i is a prefix, it will increment the value of i first , and then use (or return) the incremented value. In this example i will first increment by 1 and then j will be set equal to this new value of i.

> i = 10;
10
> j = ++i;
11
> i
11
> j
11

I am a bit confused by the first example, wont i++ assign the value to j ? so the idea is j = i++, (j = i at this point), and then increment i, so i + 1 while nothing changes to j.