+= javascript What does it mean?

dllll

Registered Member
Joined
Jun 20, 2014
Messages
89
Reaction score
39
Im learning javascript and im confused on what this means +=
I know the + is concatenation and = is equals but im still confused, i looked it up of course on w3schools but im still confused lol?
Its being used between 2 variables on the code im editing.
 
That's for appending value to a variable...

example..
Code:
var1 = 'a';
var1 += 'b';
alert(var1)
// var1 will now become ab

it is essentially the same code as

Code:
var1 = 'a';
var1 = var1+ 'b';
alert(var1);
//var1 will now become ab
 
Thanks i understand it now :)
 
g0g0l cleared it up for you.

Just FYI its .= in php.

Adds something to the existing value of the variable.
 
Note that if your variables are numbers, it's an shorthand for addition.

Code:
var a=1,b=2;
b += a; // => 3
// Same as b = b + a;

Just remember this: for strings + is concatenation and for numbers it's addition. This is called operator overloading (same operator acts differently depending on the operand types).
 
Back
Top