Parsing the string "009" as an integer in javascript?

how to parse 009 in javascript i need to return the value as 9 , but it returns 0.
But when I parse 001, it returns 1 .

var tenant_id_max = '3-009'; tenant_id_split = tenant_id_max.split("-"); var tenant_id_int = tenant_id_split[1]; var tenant_id_count = parseInt(tenant_id_int); 
+8
javascript
source share
1 answer

Do

 var tenant_id_count = parseInt(tenant_id_int, 10); 

This is because the line starting with β€œ0” is parsed as octal (which doesn’t work very well for β€œ009”, therefore you get 0) if you did not specify a radius.

From MDN :

If the input line starts with "0", the radius is eight (octal). This function is non-standard, and some implementations do not knowingly support it (use radix 10 instead). For this reason, always specify the radius when using parseInt.

The most important thing to remember: Always indicate the radius .

+18
source share

All Articles