Sony Hossain Sony Hossain - 1 month ago 18
Javascript Question

Need to learn some javascript codes!! Would you help me?

It can be a simple question here for many of you but for me it's a tough one!!
Anyways, I'd like to know why pos+=1; is used instead of pos=+1; in javascript..when we go to make an animation?? Thanks!!

Answer

pos += 1 will increment the variable pos by 1, and return it.

pos = +1 is the same as saying pos = 1. The plus is redundant.

See it in action with this simple script:

pos1 = 10;
pos2 = 10;

pos1 += 1;
pos2 =+ 1;

console.log('pos1', pos1);
console.log('pos2', pos2);