Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does parseInt return NAN for "08" string and return 7 for "07" string?

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

like image 485
br araujo Avatar asked Jun 20 '13 02:06

br araujo


1 Answers

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.

like image 73
Adam Rackis Avatar answered Sep 28 '22 10:09

Adam Rackis