它将打印console.log(1+ +"2")的内容 [英] what it will print console.log(1+ + "2")

查看:91
本文介绍了它将打印console.log(1+ +"2")的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样的JavaScript语句:

Why does this JavaScript statement:

console.log(1 +  + "2");

打印

3

作为输出?我不确定为什么要打印此文件-我期望为"12".

as the output? I am not sure why it's printing this - I expected "12".

推荐答案

+ -操作数将其转换为数字.所以这里 +"2" 将变成 2 ,因此结果将是 3 .

+ or - operand in front of a string converts it to number. so here +"2" will become 2 hence the result will be 3.

=> 1 + + "2"    // +"2" = 2
=> 1 +    2
=> 3

如果您在

=> 1 - - "2"   // -"2" = -2
=> 1 - - 2     // 1 - (-2)
=> 1 + 2
=> 3

所以

     -"2" ==> -2
     +"2" ==>  2
 +"Hello" ==> NaN
 -"Hello" ==> NaN

这篇关于它将打印console.log(1+ +"2")的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆