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 Now100% browser-based • No upload to server • No sign-up required
How to Base64 Encode in JavaScript — Complete Guide
- For simple ASCII text: use btoa(text) to encode, atob(encoded) to decode
- For UTF-8 text: use btoa(unescape(encodeURIComponent(text)))
- For Node.js: use Buffer.from(text).toString("base64")
- For files: use FileReader.readAsDataURL() for data URI encoding
- Test your encoding with our free tool above
Pro Tips
- btoa() only handles ASCII (code points 0-255) — it throws on emojis and non-Latin text
- Modern approach: new TextEncoder().encode(text) + custom Base64 function
- Node.js Buffer.from() handles all encodings natively — prefer it over btoa()
- For React/Vue: import { Buffer } from "buffer" polyfill for browser use
Frequently Asked Questions
Related Tools & Guides
Ready to Use Base64 Encoder & Decoder?
Free, instant, and 100% private. No sign-up needed.
Open Base64 Encoder & Decoder