1

I found this weird syntax in d3 example

var heatmapChart = function(tsvFile) {
    d3.tsv(tsvFile,
    function(d) {
      return {
        day: +d.day,
        hour: +d.hour,
        value: +d.value
      };
    },

What plus in

      day: +d.day,

mean?

kurumkan
  • 2,256
  • 3
  • 19
  • 51
  • @Bergi it's not a duplicate at all. – kurumkan Oct 10 '16 at 18:34
  • How so? Doesn't the duplicate explain what `+` does in front of a value? Or are you asking about something else? – Bergi Oct 10 '16 at 18:37
  • 1
    You might also want to take a look at [What's the significant use of unary plus and minus operators?](http://stackoverflow.com/q/5450076/1048572) – Bergi Oct 10 '16 at 18:38

1 Answers1

0

This is a unary conversion. It's a shorthand method to convert to a number. From the ECMAScript docs:

The unary + operator converts its operand to Number type.

The production UnaryExpression : + UnaryExpression is evaluated as follows:

Let expr be the result of evaluating UnaryExpression.

Return ToNumber(GetValue(expr)).

var a = "45"; //typeof String
a = +a; // now typeof Number
Community
  • 1
  • 1
Sterling Archer
  • 20,452
  • 15
  • 77
  • 107