ArslArsl ArslArsl - 1 year ago 115
Javascript Question

Js object key: plus value syntax

I found this weird syntax in d3 example

var heatmapChart = function(tsvFile) {
function(d) {
return {
hour: +d.hour,
value: +d.value

What plus in



Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download