为什么 post-increment 运算符返回 NaN?
Why NaN is returned by a post-increment operator?
当在 node.js 控制台中执行以下代码行时,结果为:
var string = 'abc'; string++;
// NaN
string;
// NaN
我认为它应该像这样工作:
var string = 'abc'; string++;
// 'abc';
string;
// NaN
我的理由:
Because ++
is a post-increment operator, meaning it returns the old value (in this case abc
) and then it adds 1
to the string, gets abc1
, which is Not A Number, but the ++
operator should return a nubmer, so it returns specialNaN
number and assigns it to variable string
请指出我哪里错了
++
如果用于字符串,首先尝试将其转换为数字,如果转换失败则得到 NaN
.
++
"postfix" 增量运算符的工作方式涉及首先执行到数字类型的转换。该转换的值始终是操作的 return 值,即使该值是 NaN
.
当在 node.js 控制台中执行以下代码行时,结果为:
var string = 'abc'; string++;
// NaN
string;
// NaN
我认为它应该像这样工作:
var string = 'abc'; string++;
// 'abc';
string;
// NaN
我的理由:
Because
++
is a post-increment operator, meaning it returns the old value (in this caseabc
) and then it adds1
to the string, getsabc1
, which is Not A Number, but the++
operator should return a nubmer, so it returns specialNaN
number and assigns it to variable string
请指出我哪里错了
++
如果用于字符串,首先尝试将其转换为数字,如果转换失败则得到 NaN
.
++
"postfix" 增量运算符的工作方式涉及首先执行到数字类型的转换。该转换的值始终是操作的 return 值,即使该值是 NaN
.