Why does parseInt return NAN for “08” string and r

2019-06-15 10:57发布

I created a simple apps script as follows:

function testeBug() {
  Logger.log(parseInt("07"));
  Logger.log(parseInt("08"));
}

And here's the logger output:

[13-06-19 23:09:13:130 BRT] 7.0 [13-06-19 23:09:13:130 BRT] NaN

Why this is happening? I'm using Google Apps Script

2条回答
Explosion°爆炸
2楼-- · 2019-06-15 11:31

You need to pass in the radix parameter to parseInt

parseInt("08", 10);

Failure to do so causes some browsers to treat strings with a leading zero as base-8, which is what you're seeing, since 07 in base-8 is 7, while 08 is invalid.

查看更多
走好不送
3楼-- · 2019-06-15 11:34

07 is valid octal notation, 08 is not.

查看更多
登录 后发表回答