[X]

parseInt Reads 0x As Hex

luljs 1999 still works

parseInt reads a leading 0x as hexadecimal on its own, but only when you leave the radix off.

In 2026: Still works. With no radix argument, parseInt inspects the string and treats a leading 0x or 0X as base 16, so parseInt('0x10') is 16. Pass an explicit radix of 10 and the same string returns 0, because base 10 has no x and parseInt stops at the first character it cannot use. Always pass the radix you mean.

Where it came from: David Flanagan, JavaScript: The Definitive Guide, the parseInt entry documenting the 0x auto-detection (6th edition, around 2011). MDN

<script>
document.write('<p><code>parseInt("0x10")</code> is <b>' + parseInt("0x10") + '</b> (the 0x makes it hex)</p>');
document.write('<p><code>parseInt("0x10", 10)</code> is <b>' + parseInt("0x10", 10) + '</b> (base 10 stops at the x)</p>');
document.write('<p><code>parseInt("0xFF")</code> is <b>' + parseInt("0xFF") + '</b></p>');
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›