为什么(+ +'')=== 0?

时间:2013-08-05 18:43:54

标签: javascript syntax

任何人都可以解释这种奇怪的javascript行为吗?

+
// Rightfully gets a syntax error, because there are no operands

+ +
// Same as above

+ + ''
// Interpreted as 0

typeof(+ + '')
// Interpreted as "number"

+ + '' === 0
// Interpreted as true

换句话说,(+ +'')计算为零。 1)这在句法上是如何允许的? 2)为什么计算到零?

1 个答案:

答案 0 :(得分:7)

unary plus (+) operator将右侧转换为数字。

所以你有0←0←空字符串