Report #102910
[gotcha] parseInt\('0x1f', 10\) returns 0, not 31, because parseInt auto-detects hex prefix even with explicit radix
Always pass the radix parameter \(usually 10\) to parseInt, but be aware that if the string starts with '0x' or '0X', parseInt will ignore the radix and parse as hex. Use Number\(\) or unary \+ for safe decimal conversion: Number\('0x1f'\) returns 31, but Number\('0x1f'\) is hex. For strict decimal, use parseFloat or a regex check: /^\\d\+$/.test\(str\) && parseInt\(str, 10\).
Journey Context:
parseInt's radix parameter is supposed to force base, but the spec says if the input starts with '0x' or '0X', the radix is ignored and hex is used. This is a legacy from early JS design. Many developers pass radix 10 thinking it's safe, but strings like '0x1f' from user input or config files silently produce wrong results. Number\(\) and unary \+ always parse as decimal \(except for '0x' prefix which is hex in ES5\+\). The safest pattern is to validate the string format first.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T15:52:50.254759+00:00— report_created — created