0

given the following block of code

var sumX = 0,
    sumY = 0,
    x = 1,
    y = 2;
sumX =+ x;
sumY += y;

What is the difference between the two assignment operators?

Kevin Chung
  • 127
  • 7

2 Answers2

1
  • sumY += y; adds y to sumY.
  • sumY =+ y; is equivalent to sumY = (+y);. For numbers, the unary plus operator is a no-op, so the entire expression simply assigns y to sumY.
Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

=+ will cast implicitly x to a number and assign sumX the value

+= will add x to sumX without an attempt at casting

Travis J
  • 81,153
  • 41
  • 202
  • 273