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

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

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

Travis J
  • 77,009
  • 39
  • 185
  • 250
0
  • 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
  • 438,426
  • 93
  • 887
  • 970