Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › Uint8Array.toBase64()

ECMAScript 2026

⚠️ Baseline Newly Available (Sept 2025)

Uint8Array.prototype.toBase64()

Encode binary data as base64. Supports URL-safe alphabet.

Last updated: October 2026

Definition: uint8Array.toBase64(options?) returns a base64-encoded string based on the data in the Uint8Array. Supports the URL-safe alphabet and optional padding omission. Replaces the awkward btoa() workaround for binary data [citation:11].

🎬 Live Example

Five common toBase64 patterns. Click any button to see the output.

Input:

                
Output:

            

💡 toBase64 replaces btoa for binary data.

📝

Basic Syntax

uint8Array.toBase64()
uint8Array.toBase64(options)

// options.alphabet: "base64" (default) | "base64url"
// options.omitPadding: boolean (default: false)
// Returns: base64-encoded string

🎯 Syntax & Parameters

Option Type Description
alphabetstring"base64" (default) or "base64url".
omitPaddingbooleanRemove trailing = padding. Default: false.
1

Basic Encoding

const bytes = new Uint8Array([29, 233, 101, 161]);
bytes.toBase64();
// "HelloQ=="

// Much cleaner than btoa:
// btoa(String.fromCharCode(...bytes))

Pro tip: toBase64() works directly on Uint8Array — no binary string conversion needed.

2

URL-Safe Alphabet

const bytes = new Uint8Array([46, 139, 222, 255, 42, 46]);

bytes.toBase64({ alphabet: "base64url" });
// "Love_you"

// Standard base64 would use + and /
// base64url uses - and _ — safe for URLs
3

Omit Padding

const bytes = new Uint8Array([29, 233, 101, 161]);

bytes.toBase64();
// "HelloQ=="

bytes.toBase64({ omitPadding: true });
// "HelloQ"
4

Roundtrip with fromBase64

const original = new Uint8Array([1, 2, 3]);

const encoded = original.toBase64();
// "AQID"

const decoded = Uint8Array.fromBase64(encoded);
// Uint8Array([1, 2, 3])

// Lossless — data preserved exactly
5

Real-World: URL-Safe Token

// Generate a cryptographically random token
const bytes = crypto.getRandomValues(new Uint8Array(32));

const token = bytes.toBase64({
  alphabet: "base64url",
  omitPadding: true
});

// Use in a URL
const url = `https://api.example.com/reset?token=${token}`;

🎯 toBase64 vs btoa vs toHex

Method Input Use when
toBase64Uint8ArrayBinary data → base64 string
btoaBinary stringLegacy — needs string conversion
toHexUint8ArrayHuman-readable hex output

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does Uint8Array.toBase64() do in JavaScript?

Uint8Array.toBase64() returns a base64-encoded string based on the data in the Uint8Array. It supports base64url alphabet and optional padding omission.

How is toBase64() different from btoa()?

btoa() requires a binary string and doesn't support base64url. toBase64() works directly on Uint8Array, supports the URL-safe alphabet, and can omit padding. Use toBase64() for binary data.

When was Uint8Array.toBase64() added to JavaScript?

Uint8Array.toBase64() was approved as part of ECMAScript 2026. It reached Baseline Newly Available in September 2025 and is available in Chrome 140+, Firefox 133+, Safari 18.2+.

What is the difference between toBase64 and toHex?

toBase64() returns a base64-encoded string (about 4 characters per 3 bytes). toHex() returns a hex-encoded string (exactly 2 characters per byte). Use base64 for compactness, hex for readability.

Does toBase64() modify the original Uint8Array?

No. toBase64() returns a new string. The Uint8Array is unchanged.

🎯 What's Next?

← All JavaScript Reference