以下为ChatGPT自动生成的解答,正确与否请自行判断。 也许你也可以从大家的发帖中找到想要的答案!
BFE.devAI解答 - JavaScript 小测验
14. Addition vs Unary Plus
输出结果:
3
3
3
12
3
12
3
2
2
1true
1NaN
1
1
1null
1NaN
NaN
NaN
1undefined
NaN
1undefined
NaN
NaN
解释:
1 + 2
的结果是 3。+ 2
表示正数 2,所以1 + + 2
的结果也是 3。+ + 2
相当于+ (+2)
,两个正号无效,所以1 + + + 2
的结果也是 3。'2'
是字符串类型,所以1 + '2'
会把数字 1 转换成字符串,最后结果是'12'
。+ '2'
表示把字符串'2'
转换成数字 2,所以1 + + '2'
的结果是 3。'1' + 2
会把数字 2 转换成字符串,最后结果是'12'
。+ 2
表示正数 2,所以'1' + + 2
的结果是字符串'12'
。true
表示数字 1,所以1 + true
的结果是 2。+ true
表示数字 1,所以1 + + true
的结果也是 2。true
转换成字符串是'true'
,所以'1' + true
的结果是字符串'1true'
。+ true
表示数字 1,所以'1' + + true
的结果是字符串'11'
。null
转换成数字是 0,所以1 + null
的结果是 1。+ null
是数字 0,所以1 + + null
的结果也是 1。null
转换成字符串是'null'
,所以'1' + null
的结果是字符串'1null'
。+ null
是数字 0,所以'1' + + null
的结果是字符串'10'
。undefined
转换成数字是 NaN,所以1 + undefined
的结果是 NaN。+ undefined
也是 NaN,所以1 + + undefined
的结果也是 NaN。undefined
转换成字符串是'undefined'
,所以'1' + undefined
的结果是字符串'1undefined'
。+ undefined
是 NaN,所以'1' + + undefined
的结果也是字符串'1NaN'
。+ + + undefined
相当于+ (+ (undefined))
,三个正号无效,所以结果是 NaN。