1

I've been looking online for this answer, and it's so simple so I'm embaressed for asking, but what exactly does the "+=" do in javascript. I don't know why but I can't seem to find this on google. Please help.

tingytong
  • 57
  • 1
  • 10

3 Answers3

4

It is syntactic sugar that allows you to add something to a variable and store the result in that variable, that is

x += 2;

is equivalent to

x = x + 2;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

Suppose

var i = 5;

You can add 6 to i by writing

i = i + 6;

Or the equivalent short-hand way:

i += 6;
Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
1

a += b; is a short form of a = a + b;

c-smile
  • 26,734
  • 7
  • 59
  • 86