0

What does the sign + means?

Example of it's use:

var svg = d3.select("svg"),
  width = +svg.attr("width"),
  height = +svg.attr("height");
user7393973
  • 2,270
  • 1
  • 20
  • 58
Rawr
  • 71
  • 1
  • 3
  • 10
  • 3
    It's worth mentioning that those D3 getters (`svg.attr("width")` and `svg.attr("height")`) return **strings**, not numbers, even if the SVG `width` and `height` are numbers. That's why you have to use the unary plus operator. – Gerardo Furtado Feb 10 '17 at 14:31

1 Answers1

1

Pragmatically, it's JavaScript shorthand for converting a value to a Number. Technically, it's the unary plus operator, complementary to the unary negation operator.

let number = "1"

console.log(typeof number)

console.log(typeof +number)
console.log(+number)

console.log(typeof -number)
console.log(-number)

console.log(typeof +true)
console.log(+true)
msanford
  • 11,803
  • 11
  • 66
  • 93
  • 1
    I see.. I didn't know that + alone can be used for typecasting. Thank you! – Rawr Feb 10 '17 at 14:39
  • @Rawr I didn't either until I saw it in a code-base and asked the same question you did. I do find it rather odd to see a construct like `1 + +var`... – msanford Feb 10 '17 at 14:44