Skip to content
Hex Calculator

JavaScript Number to Hex Converter

Convert a JavaScript number to its hexadecimal string form — the same conversion Number.prototype.toString(16) performs.

Hex value
0xFF
Swapnil Sanghvi

Built by

Swapnil Sanghvi

Full-Stack Web Developer & WordPress Developer

Swapnil Sanghvi is a full-stack web and WordPress developer, UI designer, and full-time freelancer who builds and maintains Hex Calculator.

JavaScript Number to Hex Converter tool preview card from Hex Calculator
Share preview: this is the card that appears when you share the JavaScript Number to Hex Converter page on social media.

Further reading: hex — Stack Overflow

How JavaScript Converts a Number to Hex

Calling toString(16) on a number converts it to base 16, returning just the digits with no 0x prefix. Since JavaScript numbers are IEEE 754 doubles, the conversion is exact for integers up to 2^53-1 — the same boundary that matters when converting the other way.

Where JavaScript Number to Hex Actually Comes Up

Formatting a Color Value as a Hex Code

Converting individual RGB numbers to their 2-digit hex form and concatenating them is a common step when building a hex color string dynamically in JS.

r.toString(16).padStart(2, '0')

Logging a Computed Value in Hex for Debugging

Converting a bitmask or hash value to hex before logging it often makes it easier to read and compare than the equivalent decimal number.

console.log(value.toString(16))

Building a Hex-Encoded Identifier

Converting a numeric ID or counter to hex to build a compact string identifier, since hex represents the same range of values in fewer characters than decimal.

id.toString(16) -> compact hex ID

Why Use This Calculator Instead of Doing It by Hand

  • Matches the result of JavaScript's own .toString(16) method
  • Runs entirely in your browser — nothing you type gets sent anywhere
  • Saves manually converting and formatting the output
  • Useful for checking expected output before writing test assertions

Limitations

  • Adds a 0x prefix and uses uppercase digits — JavaScript's own toString(16) returns lowercase digits with no prefix, so adjust case/prefix to match your code's exact expectations.
  • Only exact for values within the safe integer range (up to 2^53-1) — larger values should use a BigInt in JavaScript instead of a plain Number.

Frequently Asked Questions

How do I convert a number to hex in JavaScript?

Call .toString(16) on the number — (255).toString(16) returns "ff". Prepend "0x" yourself if you want the literal-style prefix.

Does this work for any JavaScript number?

It's exact for integers within the safe integer range (up to 2^53-1, Number.MAX_SAFE_INTEGER) — beyond that, JavaScript's Number type itself can't represent every integer exactly, so the conversion would already be working from an imprecise value.

How do I convert a BigInt instead of a Number?

The same .toString(16) method works on a BigInt too — (255n).toString(16) returns "ff", and BigInts have no upper size limit, unlike regular Numbers.

Does toString(16) pad with leading zeros?

No — it produces the shortest representation, so (1).toString(16) is just "1", not "01". Pad manually with padStart() if you need a fixed width.