ECMAScript 2026
⚠️ Baseline Newly AvailableUint8Array.prototype.toHex()
Encode binary data as a hex string. Pairs with fromHex for roundtrips.
Last updated: October 2026
Definition: uint8Array.toHex() returns a hexadecimal string representation of the data. Each byte becomes exactly two hex characters. Pairs with Uint8Array.fromHex() for lossless roundtrips [citation:1][citation:20].
🎬 Live Example
Five common toHex patterns. Click any button to see the output.
💡 toHex is 2 chars per byte — human-readable but larger than base64.
Basic Syntax
uint8Array.toHex()
// Returns: lowercase hexadecimal string
// 2 characters per byte
// Inverse: Uint8Array.fromHex(hexString)
🎯 Parameters & Return
| Parameter | Type | Description |
|---|---|---|
| — | — | Takes no arguments. |
| Return | string | Lowercase hex representation of the bytes. |
Basic Encoding
const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
bytes.toHex();
// "48656c6c6f"
// Same as "Hello" in UTF-8
Roundtrip with fromHex
const original = new Uint8Array([1, 2, 255]);
const hex = original.toHex();
// "0102ff"
const decoded = Uint8Array.fromHex(hex);
// Uint8Array([1, 2, 255])
// Lossless — data preserved
Pro tip: fromHex is the exact inverse of toHex. Perfect for roundtrips.
Binary Data Representation
const bytes = new Uint8Array([0, 1, 15, 16, 255]);
bytes.toHex();
// "00010f10ff"
// Each byte = exactly 2 hex chars
// 0x00 → "00", 0xff → "ff"
Hash Display
// Display a cryptographic hash
const bytes = crypto.getRandomValues(new Uint8Array(8));
const hex = bytes.toHex();
// "a3f1c9b8d4e2f761"
// Fixed length — predictable output
Real-World: Compare Bytes
const a = new Uint8Array([1, 2, 3]);
const b = new Uint8Array([1, 2, 4]);
a.toHex() === b.toHex();
// false
// Quick equality check via hex strings
🎯 toHex vs toBase64 vs toString
| Method | Size per byte | Use when |
|---|---|---|
toHex | 2 chars | Readable, predictable output |
toBase64 | ~1.33 chars | Compact transport (URLs, tokens) |
toString | Varies | Debugging (comma-separated) |
⚠️ Common Mistakes
- Assuming uppercase output. toHex returns lowercase. Use
.toUpperCase()if needed. - Using it on regular arrays. Only Uint8Array has toHex.
- Assuming old browser support. Baseline 2026 — check compatibility.
- Using hex for network transport. base64 is ~33% smaller. Use hex for debugging only.
- Forgetting fromHex exists. Don't manually parse hex strings — use the built-in inverse.
❓ Frequently Asked Questions
What does Uint8Array.toHex() do in JavaScript?
Uint8Array.toHex() returns a hexadecimal string representation of the data in the Uint8Array. Each byte becomes exactly two hex characters.
What is the difference between toHex and toBase64?
toHex produces 2 characters per byte (readable but larger). toBase64 produces about 4 characters per 3 bytes (compact). Use hex for debugging, base64 for transport.
When was Uint8Array.toHex() added to JavaScript?
Uint8Array.toHex() and fromHex() were approved as part of ECMAScript 2026 (June 2026). They reached Baseline Newly Available in 2026.
How do I decode a hex string back to bytes?
Use Uint8Array.fromHex(hexString). It is the inverse of toHex() and is also part of ECMAScript 2026.
Does toHex() modify the original Uint8Array?
No. toHex() returns a new string. The Uint8Array is unchanged.