Base64 in JavaScript: btoa, atob, and Buffer Explained
Encode and decode Base64 in JavaScript using btoa/atob in browsers and Buffer in Node.js. Covers Unicode gotchas and URL-safe Base64 patterns.
Published:
Tags: encoding, javascript, base64
Base64 in JavaScript: btoa, atob, and Buffer Explained JavaScript has three different Base64 APIs depending on where your code runs: / in browsers, in Node.js, and -based approaches for handling Unicode correctly. This guide covers all three, explains why breaks on non-ASCII strings, and shows you the right pattern for each environment. --- Browser: btoa and atob (binary to ASCII) encodes a string to Base64. (ASCII to binary) decodes Base64 back to a string. Both are available in every modern browser and in Node.js 16+. Simple — but there's a critical limitation. The Unicode problem with btoa only handles characters in the Latin-1 (ISO-8859-1) range, meaning code points 0–255. Any character outside that range throws a . The correct Unicode-safe approach Encode the string to UTF-8 bytes…
All articles · theproductguy.in