sopanha
sopanha

Reputation: 345

why do spaces break my code in javascript?

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

Answers (4)

LiverpoolsNumber9
LiverpoolsNumber9

Reputation: 2394

The JavaScript engine interprets "+ =" differently from "+=". That's just the way it is written.

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 148990

Because += is an augmented (or compound) assignment operator, not + =. Similarly, i++ is fine, but i+ + causes a syntax error.

Upvotes: 3

Eugene P.
Eugene P.

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

spiehr
spiehr

Reputation: 411

+= is an operator. It is not a combination of + and =.

Upvotes: 5

Related Questions