Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is JSlint error "missing radix parameter" in JavaScript?
The parseInt function in JavaScript has the following signature:
parseInt(string, radix);
Where the parameters are:
string ? The value to parse. If this argument is not a string, then it is converted to one using the ToString method. Leading whitespace in this argument is ignored.
radix ? An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string.
The Problem: Omitting the Radix Parameter
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)
Example: Unexpected Behavior Without Radix
// Without radix - can cause unexpected results
console.log(parseInt("010")); // 8 (octal) in older browsers
console.log(parseInt("0x10")); // 16 (hexadecimal)
console.log(parseInt("10")); // 10 (decimal)
8 16 10
Solution: Always Specify the Radix
// With radix - predictable behavior
console.log(parseInt("010", 10)); // 10 (decimal)
console.log(parseInt("010", 8)); // 8 (octal)
console.log(parseInt("0x10", 16)); // 16 (hexadecimal)
console.log(parseInt("10", 10)); // 10 (decimal)
10 8 16 10
Common Use Cases
// Converting user input to decimal let userInput = "123"; let number = parseInt(userInput, 10); console.log(number); // 123 // Converting binary string let binary = "1010"; let decimal = parseInt(binary, 2); console.log(decimal); // 10 // Converting hexadecimal let hex = "FF"; let decimalFromHex = parseInt(hex, 16); console.log(decimalFromHex); // 255
123 10 255
Why JSLint Reports This Error
JSLint reports the "missing radix parameter" error to prevent unexpected behavior caused by automatic radix detection. This ensures consistent parsing across different JavaScript environments and prevents bugs related to octal interpretation.
Conclusion
Always specify the radix parameter in parseInt() to avoid unpredictable results. Use radix 10 for decimal numbers, which is the most common use case.
