Base64 in JavaScript
How to encode and decode Base64 in JavaScript — with copy-paste examples and a live converter to check your output.
In the browser, JavaScript uses the built-in btoa() and atob() functions; in Node.js you use the Buffer class. Wrap btoa/atob for full UTF-8 support.
Encode to Base64 in JavaScript
// Browser (UTF-8 safe)
const encoded = btoa(unescape(encodeURIComponent("Hello, World!")));
// Node.js
const encoded = Buffer.from("Hello, World!", "utf-8").toString("base64");Decode Base64 in JavaScript
// Browser (UTF-8 safe)
const text = decodeURIComponent(escape(atob(encoded)));
// Node.js
const text = Buffer.from(encoded, "base64").toString("utf-8");Notes & gotchas
btoa() and atob() only handle Latin-1 directly, so the encodeURIComponent/unescape wrapper is required for emoji and non-ASCII text. In Node.js, Buffer handles UTF-8 natively.
Try it live
Base64 in JavaScript — FAQ
How do I Base64 encode a string in JavaScript?+
Use the code shown above. btoa() and atob() only handle Latin-1 directly, so the encodeURIComponent/unescape wrapper is required for emoji and non-ASCII text. In Node.js, Buffer handles UTF-8 natively.
How do I decode Base64 back to text in JavaScript?+
Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.
Is Base64 encoding the same across programming languages?+
Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.
Does Base64 secure my data?+
No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.