为什么javascript console.log(1 + +""+ 3); 句子返回4而不是"1 3"?

0 javascript coercion console.log

console.log(1 + + " " + 3);在Chrome控制台中执行语句时,结果4并非"1 3"如我所料.

有人可以解释为什么会这样吗?

jhe*_*rax 5

此行为称为强制.

在这种情况下,一元加运算符+将右侧的表达式转换为数字.如果它无法解析特定值,它将评估为NaN.

+ " " //--> is coerced to 0
Run Code Online (Sandbox Code Playgroud)

你可以在这个Gist中看到一些强制性的例子: JavaScript强制