1

I'm looking at the source code of the sum function of d3 and I'm wondering what the + in line 8 and 10 does. It's this statement:

a = +array[i]

So it's an assignment of the ith element of array to a, but what does the + in front of array[i] do? The same syntax is repeated in line 10, where the + precedes the function call. I have never seen such a syntax in JavaScript.

crito
  • 23
  • 2

1 Answers1

1

What you're really doing is applying the operator + to the element at index i in the array. And what does that operator do? Basically, it's a shorthand way of forcibly turning a variable into a number. As an example, run the following code:

+new Date()

Instead of a date object, you'll get an integer representation of the date.

Andreas Eriksson
  • 8,687
  • 7
  • 44
  • 64