NaN Issue -parseInt() throwing NaN for '08' and '09' number.

Community Alums
Not applicable
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 05:00 AM
NaN issue has been observed when parsing a value '08' or '09' by using parseInt() . Though the existing logic was working fine for value between '01' to '07' number range. For ex. I have tried this sample code in background script of servicenow instance.
1) var a='09';
var b=parseInt(a);
gs.info(b);
var b=parseInt(a);
gs.info(b);
Result: NaN.
2) var a='06';
var b=parseInt(a);
gs.info(b);
var b=parseInt(a);
gs.info(b);
Result: 6.
Could you please suggest the work around or solution for above NaN issue?
Labels:
- Labels:
-
User Experience and Design
3 REPLIES 3

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 05:03 AM
Hi,
Did you try using parseFloat() instead of parseInt() for a check.

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 05:09 AM
Hi,
By forcing parseInt to use 10-base values, it can be done. Like this
var a='09';
var b=parseInt(a, 10);
gs.info(b);
// output is 9

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2022 05:13 AM
For more information on why this is happening, visit w3schools