Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lint warning: parseInt missing radix parameter [duplicate]

I have the following code that gives the warning described in the title:

          year: parseInt(dateParts[0]),
               ......................^

Any help is much appreciated

like image 922
user1723894 Avatar asked Jul 16 '13 16:07

user1723894


1 Answers

See the manual for parseInt; it takes 2 arguments. The second one tells it which number base you want to use. This is almost always going to be 10 (decimal).

parseInt(dateParts[0],10)

If you don't specify it, then it will be inferred from the data.

If radix is undefined or 0 (or absent), JavaScript assumes the following:

If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.

If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

If the input string begins with any other value, the radix is 10 (decimal).

Some years after I wrote the above, things have changed. Now:

The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values. Many implementations have adopted this behavior as of 2021.

… but you still should use a radix, at least for browser side code, because not all implementations have caught up.

like image 131
Quentin Avatar answered Oct 04 '22 07:10

Quentin