JavaScript中符号“+”的含义?

时间:2017-02-10 14:21:28

标签: javascript

符号+的含义是什么?

使用示例:

var svg = d3.select("svg"),
  width = +svg.attr("width"),
  height = +svg.attr("height");

1 个答案:

答案 0 :(得分:1)

实际上,它是将值转换为Number的JavaScript简写。从技术上讲,它是unary plus operator,是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)