Reputation: 345
why is el+ = v[i]
different from el+=v[i]
?
i thought javascript doesn't care about spacing.
thanks, Any answers are appreciated it. and please don't put my question on hold. i'm here trying to learn.
var v = ['a','b','c','d','e'];
var el="";
for(i=0; i<v.length; i++){
el+ = v[i]; // not working because of spaces, but why?
// el+=v[i]; // working
}
document.write(el);
Upvotes: 1
Views: 91
Reputation: 2394
The JavaScript engine interprets "+ =" differently from "+=". That's just the way it is written.
Upvotes: 2
Reputation: 148990
Because +=
is an augmented (or compound) assignment operator, not + =
. Similarly, i++
is fine, but i+ +
causes a syntax error.
Upvotes: 3
Reputation: 2625
el+ =
it's illegal operator.
+=
-=
/=
*=
are available for instance
for your case. I will suggest even avoid for loop and do instead :
var el = v.join('');
Upvotes: 2