- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2021 09:21 PM
Tried multiple combinations in background script. but parseInt('08') and parseInt('09') unfortunately returns NaN.
This is weird. Can anyone put your brain on this and tell me what is going wrong?
Best Regards,
Arun
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2021 09:42 PM
That is not entirely true.
This happens duo to ServiceNow ES3 (very old engine) and have been fixed on newer engines.
Older browsers will result parseInt("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
This means that parseInt('010') returns 8 when evaluated in global scope but if done in a scope then it works as it should
But as you state - the best way to ensure this is to use the 2nd parameter to ensure that it always evaluated as decimal ( parseInt('010', 10) ).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2021 09:27 PM
var myNumber = '0009';
gs.info(parseInt(myNumber , 10));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2021 09:29 PM
The problem is leading zeroes confuses parseInt();
So, specify radix as 10 in parseInt().
10 is the radix, and should be always specified to avoid inconsistencies across different browsers, although some engines work fine without it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2021 09:42 PM
That is not entirely true.
This happens duo to ServiceNow ES3 (very old engine) and have been fixed on newer engines.
Older browsers will result parseInt("010") as 8, because older versions of ECMAScript, (older than ECMAScript 5, uses the octal radix (8) as default when the string begins with "0". As of ECMAScript 5, the default is the decimal radix (10).
If the radix parameter is omitted, JavaScript assumes the following:
If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)
This means that parseInt('010') returns 8 when evaluated in global scope but if done in a scope then it works as it should
But as you state - the best way to ensure this is to use the 2nd parameter to ensure that it always evaluated as decimal ( parseInt('010', 10) ).