Difference between i+=5 and i+5 inside a for loop?


#1

My understanding is below code should increment the value of i by 5 with i+5, There was no compile error or a syntax error, but my browser crashed. should I always use i+=5???

for (var i = 5; i < 51; i+5) {
console.log(i);
}


#2

Well i+=5 is a shortcut for i=i+5 and if you have a close look you see the problem:

Spoiler

i+5 computes a value. For example when i is 5 i+5 is simply the value of 10
i+=5 or i=i+5 on the other hand compute this value as well, but they also assign it back to the variable. So by using i+=5 the value of i gets changed.

So as i+5 doesn't change the value i will remain 5 and 5 is less than 51 ... always.. therefore the loop will run forever. Or better until the browser refuses to process it.