13

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

br araujo
  • 1,856
  • 2
  • 13
  • 14
  • 1
    possible duplicate of [How come parseInt("08") = 0, parseInt("07") = 7](http://stackoverflow.com/questions/12652305/how-come-parseint08-0-parseint07-7) - octal in JavaScript - 8 is not valid octal digit. – Alexei Levenkov Jun 20 '13 at 02:15
  • I'm using GOOGLE APPS SCRIPT not Java Script – br araujo Jun 20 '13 at 02:19
  • Altought the same solution worked for it. – br araujo Jun 20 '13 at 02:22
  • 2
    Google seem to have slightly different view of "GOOGLE APPS SCRIPT not Java Script" : "Google Apps Script **is a JavaScript** cloud scripting language that lets you extend Google Apps and build web applications" (https://developers.google.com/apps-script/overview) – Alexei Levenkov Jun 20 '13 at 02:32
  • Alexei, thanks for your explanation. I didn't notice that. +1 in your comment. – br araujo Jun 20 '13 at 13:27
  • Should be able to use new Number() in it's place – LennyZ71 Jun 21 '13 at 03:00

2 Answers2

23

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.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
1

07 is valid octal notation, 08 is not.

pilcrow
  • 56,591
  • 13
  • 94
  • 135