Convert hex to decimal with parseInt()
Call parseInt() with the hex string and radix 16. The radix tells JavaScript the string is base 16 (hexadecimal). Always pass 16 explicitly: without it, parseInt reads plain digits as decimal.
parseInt("1A3F", 16) // 6719
parseInt("ff", 16) // 255 — case does not matter
parseInt("0xFF", 16) // 255 — the 0x prefix is accepted
parseInt("0x1A3F", 16) // 6719Partial parsing and NaN
parseInt reads characters from left to right and stops at the first character that is not a valid hex digit, without throwing an error.
parseInt("FFzz", 16) // 255 — stops at "z"
parseInt("zz", 16) // NaN — no valid digits at all
parseInt("", 16) // NaN — empty stringThis silent truncation can hide bugs. Validate the input with a regular expression before parsing when the value comes from a user or an external source:
/^(0x)?[0-9a-f]+$/i.test(input)Number() with the 0x prefix
Number() converts a hex string to a number when the string starts with 0x. It rejects any character that is not part of the hex literal, so it returns NaN (Not a Number) for partial input instead of silently truncating.
Number("0xFF") // 255
Number("0x1A3F") // 6719
Number("0xGG") // NaN — invalid character
Number("FF") // NaN — no 0x prefixUse Number() when the input already has the 0x prefix and you want strict validation. Use parseInt() when the input may or may not have the prefix.
Large values and BigInt
JavaScript's Number type stores integers exactly up to 2⁵³ − 1 (9,007,199,254,740,991), which is 0x1FFFFFFFFFFFFF in hex. Past that limit, parseInt and Number round the result. A 64-bit identifier, a cryptographic hash or a memory address longer than 14 hex digits needs BigInt.
parseInt("20000000000001", 16) // 9007199254740992 — rounded
BigInt("0x20000000000001") // 9007199254740993n — exact
BigInt("0xFFFFFFFFFFFFFFFF").toString() // "18446744073709551615"BigInt needs the 0x prefix when parsing hex. The result is a BigInt value, not a Number, so it cannot be mixed with regular numbers in arithmetic without explicit conversion.
Signed 64-bit with BigInt
BigInt.asIntN() reads a BigInt as a signed two's complement value at a given bit width.
BigInt.asIntN(64, BigInt("0xFFFFFFFFFFFFFFFF")) // -1n
BigInt.asIntN(32, BigInt("0xFFFFFFFF")) // -1n
BigInt.asIntN(16, BigInt("0xFF9C")) // -100nSigned hex to decimal
The bitwise OR operator | 0 forces a value into a signed 32-bit integer, which gives the two's complement reading.
parseInt("FFFFFFFF", 16) | 0 // -1
parseInt("80000000", 16) | 0 // -2147483648
parseInt("FF9C0000", 16) | 0 // -6553600For other bit widths, write the value into a typed array and read it back:
new Int8Array([0xFF])[0] // -1 (8-bit signed)
new Int16Array([0xFF9C])[0] // -100 (16-bit signed)
new Int32Array([0xFFFFFFFF])[0] // -1 (32-bit signed)Typed arrays interpret the bits at the array's fixed width, so Int8Array gives an 8-bit signed result and Int16Array gives a 16-bit signed result.
For a full breakdown of sign bits and two's complement at every width, see the signed hex to decimal converter.
Handling input formats
Hex values in the wild come with different prefixes and formatting. Strip them before parsing.
function hexToDec(s) {
s = s.trim().replace(/^(0x|#)/i, "");
if (!/^[0-9a-f]+$/i.test(s)) return NaN;
return parseInt(s, 16);
}
hexToDec("0xFF") // 255
hexToDec("#FF") // 255 — CSS colour prefix
hexToDec(" ff ") // 255 — whitespace
hexToDec("GG") // NaN — invalidCSS colour codes use # instead of 0x. The function above handles both. For colour-specific conversion with RGB channels, see the hex to RGB converter.
Decimal to hex in JavaScript
Use toString(16) to convert a decimal number back to a hex string. The result is lowercase with no prefix.
(255).toString(16) // "ff"
(255).toString(16).toUpperCase() // "FF"
(255).toString(16).padStart(4, "0") // "00ff"
"0x" + (6719).toString(16).toUpperCase() // "0x1A3F"For the full reverse conversion method and worked examples, see the decimal to hex converter.
Method comparison
| Method | Needs 0x | Partial parse | Precision limit | Signed |
|---|---|---|---|---|
parseInt(s, 16) | No | Silently truncates | 2⁵³ − 1 | No |
Number("0x" + s) | Yes | Returns NaN | 2⁵³ − 1 | No |
BigInt("0x" + s) | Yes | Throws error | None | With asIntN |
parseInt(s, 16) | 0 | No | Truncates | 32-bit only | Yes (32-bit) |
Int8Array / Int16Array | No | N/A (numeric input) | Fixed width | Yes |
Start with parseInt(s, 16) for most conversions. Reach for BigInt when the hex value exceeds 1FFFFFFFFFFFFF, and use | 0 or a typed array when you need a signed result.
Type one string and compare all five methods side by side. Try FFzz for partial parsing and FFFFFFFFFFFFFFFF for precision.
Frequently asked questions
How do you convert hex to decimal in JavaScript?
parseInt("FF", 16) to convert the hex string "FF" to the decimal number 255. The second argument, 16, is the radix that tells parseInt the input is base 16.What is the radix parameter in parseInt?
parseInt reads plain digits as decimal, and only a leading 0x switches it to hex.Why does parseInt return NaN?
parseInt returns NaN when no valid digits appear at the start of the string. parseInt("zz", 16) is NaN because "z" is not a hex digit. An empty string also returns NaN.How do you convert signed hex to decimal in JavaScript?
parseInt("FFFFFFFF", 16) | 0 for 32-bit signed values, which returns −1. The bitwise OR forces the value into a signed 32-bit integer. For other widths, use new Int8Array([value])[0] or BigInt.asIntN(bits, bigintValue).How do you handle hex values larger than 2⁵³ in JavaScript?
BigInt("0x" + hexString) for exact conversion of values above 2⁵³ − 1. parseInt and Number lose precision above 1FFFFFFFFFFFFF, which means any hex string longer than 14 digits. BigInt has no size limit.Does parseInt accept the 0x prefix?
parseInt("0xFF", 16) returns 255. The 0x prefix is optional when radix 16 is specified. Number("0xFF") also works but requires the prefix.Related tools
- Hex to Decimal Converter — convert hex to decimal with digit-by-digit steps
- Hex to Decimal in Python — int(), bytes and struct methods
- Hex to RGB Converter — convert CSS hex colours to RGB channels
- Signed Hex to Decimal — two's complement at any bit width