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
  • 191,680
  • 20
  • 149
  • 239
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
  • 14,936
  • 9
  • 68
  • 98
1

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

c-smile
  • 25,702
  • 7
  • 55
  • 82