letter-jJ
×

++ or +1

I'll admit. This concept is probably such a simple one that I shouldn't even be writing about it. But, I've fallen trap to this misconception many times — alas here I am typing.

So heres the problem...

Say you have y which is the year, and you want to know what year next year is.

One may think to be terse and write:

var newYear = ++y;

Note: you may be tempted to use y++. But, y++ won't work here. Why you ask? Because of Prefix vs Postfix.

Perhaps only I am silly enough to write such things. Either way, you can probably spot the problem.

Both newYear and y are equal to y + 1.

That makes for tricky use of y later on.