Base64 Encode in JavaScript — Complete Guide

JavaScript provides multiple ways to handle Base64: btoa()/atob() for browsers, Buffer for Node.js, and TextEncoder for proper UTF-8 support. This guide covers

Open Base64 Encoder & Decoder →

JavaScript provides multiple ways to handle Base64: btoa()/atob() for browsers, Buffer for Node.js, and TextEncoder for proper UTF-8 support. This guide covers all methods with working code examples.

Frontend developers encoding images for data URIs, handling API authentication headers, decoding JWT tokens, or transmitting binary data via WebSockets all need JavaScript Base64.

Try It Now — Free, No Sign-up

Open the tool and get started instantly. No sign-up, no installation needed.

Open Base64 Encoder & Decoder Now

100% browser-based • No upload to server • No sign-up required

How to Base64 Encode in JavaScript — Complete Guide

  1. For simple ASCII text: use btoa(text) to encode, atob(encoded) to decode
  2. For UTF-8 text: use btoa(unescape(encodeURIComponent(text)))
  3. For Node.js: use Buffer.from(text).toString("base64")
  4. For files: use FileReader.readAsDataURL() for data URI encoding
  5. Test your encoding with our free tool above

Pro Tips

Frequently Asked Questions

Why does btoa() fail with special characters?
btoa() only handles Latin-1 characters (0-255). For UTF-8 (emojis, Chinese, Arabic), first encode with encodeURIComponent(), then btoa(). Or use Buffer in Node.js.
How to Base64 encode a file in JavaScript?
Use FileReader: reader.readAsDataURL(file). The result is a complete data URI (data:mime;base64,...). Split on comma to get just the Base64 part.
What is the Node.js way to do Base64?
Buffer.from(string, "utf8").toString("base64") for encoding. Buffer.from(encoded, "base64").toString("utf8") for decoding. Clean and handles all Unicode.
Is there a Base64 library for JavaScript?
For most cases, built-in btoa/atob or Buffer is enough. For Base64URL specifically, use the base64url npm package or Buffer.toString("base64url") in Node 16+.

Related Tools & Guides

Ready to Use Base64 Encoder & Decoder?

Free, instant, and 100% private. No sign-up needed.

Open Base64 Encoder & Decoder