parseInt is not parsing number in Servicenow. But works in other places as expected.

Arun101
Kilo Contributor

find_real_file.png

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

1 ACCEPTED SOLUTION

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) ).

View solution in original post

3 REPLIES 3

AnirudhKumar
Mega Sage
Mega Sage
var myNumber = '0009';

gs.info(parseInt(myNumber , 10));

AnirudhKumar
Mega Sage
Mega Sage

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.

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) ).