Report #97624
[gotcha] parseInt\('0x', 16\) returns NaN, but parseInt\('0x1', 16\) returns 1 — inconsistent hex prefix handling
Never rely on parseInt's auto-detection of radix. Always pass the radix explicitly \(usually 10\). For hex parsing, use Number\('0x...'\) or BigInt\('0x...'\) which handle the prefix correctly. For strict validation, use a regex like /^0x\[0-9a-fA-F\]\+$/ before parsing.
Journey Context:
parseInt\('0x1', 16\) works because it stops at the first invalid character \(the 'x' is valid as part of the 0x prefix when radix is 16\). But parseInt\('0x', 16\) returns NaN because after the '0x' there are no valid hex digits. Worse, parseInt\('0x', 10\) returns 0 because it parses '0' and stops at 'x'. This asymmetry is documented but surprising. The root cause: parseInt stops at the first character that is not valid for the given radix, but the '0x' prefix is only valid for radix 16. This leads to silent failures when parsing user input or malformed strings. The safer alternative is Number\(\) which returns NaN for any invalid hex string including '0x'.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-25T15:45:19.870739+00:00— report_created — created