All Recipes
StarVerified Developer Solution • 100% Offline
Base64 / Hex / Binary Multi-Inspector & Image Previewer

Fix "DOMException: The string to be decoded is not correctly encoded"

Solve atob() crashes in JavaScript when decoding invalid or unpadded Base64 strings.

The Problem (Error Root Cause)Exception

Calling `atob()` in JavaScript throws a DOMException. This occurs when the string contains characters outside the base64 alphabet, or if the padding `=` is missing/incorrect.

Identified via runtime validation & stack traces
The Solution (Step-by-Step Fix)Verified

Ensure your string length is a multiple of 4 by adding `=` padding, and remove URL-safe characters (`-` and `_`) by replacing them with `+` and `/`.

Deterministic, non-destructive resolution

Code Standard: Bad Pattern vs Verified Fix

Live Syntax
Anti-Pattern vs Verified Fix
1// ❌ Bad: URL-safe unpadded base64
2const decoded = atob("eyJhbGciOiJIUzI1NiJ");
3
4// ✅ Good: Padded and standard base64
5let b64 = "eyJhbGciOiJIUzI1NiJ".replace(/-/g, '+').replace(/_/g, '/');
6while (b64.length % 4) b64 += '=';
7const decoded = atob(b64);

Test and resolve this using Base64 / Hex / Binary Multi-Inspector & Image Previewer

Execute directly in your browser memory. Zero API keys, zero network tracking, completely client-side.

Launch Base64 / Hex / Binary Multi-Inspector & Image Previewer

Frequently Asked Questions

Q:Why does Node.js buffer not crash on this?

Node.js Buffer.from(str, "base64") is more forgiving than the browser's strict atob() implementation.

Q:How can I inspect malformed base64?

Use our Base64 Inspector which automatically handles padding and URL-safe conversions.

Related Troubleshooting Guides

View Directory