How can I subtract 1 (or any number) from the index without decrementing it (without it’s value changing in the variable) ?
Here’s a snippet of my code:
if (i > 0 & areaImages[i].id == areaImages[i-1].id)
And I get this error message from Firebug:
TypeError: areaImages[i - 1] is undefined
Nothing is decremented. The actual reason is you used the wrong operator.
For booleans, both
&and&&will return same (up to==) result. But the crucial difference is that&&is short-circuiting, i.e. when the left-hand-side of&&isfalse, then the right-hand-side will not be evaluated.The problem of the original code is that
&is not short-circuiting, soareaImages[i-1].idwill be evaluated even ifi <= 0. Buti-1is an invalid index, soareaImages[i-1]is undefined, and you cannot get a property from undefined, causing the TypeError.