JavaScript to Base64
Encode any JavaScript string to Base64 for use in HTTP headers, data URIs, API payloads, and localStorage values. Includes the equivalent btoa() browser snippet and Node.js Buffer.from() code.
Last updated: May 28, 2026
Find this tool useful? Support the project to keep it free!
Buy me a coffeeWhat is JavaScript to Base64?
JavaScript developers encode strings to Base64 when building Basic Auth headers, constructing data URIs for inline assets, preparing JWT-style payloads, or embedding binary content in JSON. The browser API is btoa(), Node.js uses Buffer.from(str).toString("base64"), and modern code often needs URL-safe output for JWT and URL parameters.
This tool encodes any string to Base64 with correct UTF-8 handling (plain btoa() breaks on non-ASCII characters) and shows the equivalent code so you can reproduce the encoding in your project.
How to Use JavaScript to Base64
Type or paste the JavaScript string you want to encode
Click "Encode to Base64" to generate the encoded output
Enable URL-safe mode if the result will appear in a URL or JWT
Copy the output or use the shown JS snippet in your code
Common Use Cases
- Frontend developers encoding credentials for HTTP Basic Auth headers in fetch() or XMLHttpRequest calls.
- React/Vue developers creating data URIs for inline SVGs or small image assets.
- Node.js engineers encoding Buffer content to Base64 for JSON-safe transmission.
- JavaScript developers encoding JSON config objects to pass as URL parameters or localStorage values.
- JWT implementers encoding header and payload objects before signing.
Example Input and Output
Encoding a JavaScript config object to Base64 for embedding in a URL parameter.
{"env":"production","debug":false}eyJlbnYiOiJwcm9kdWN0aW9uIiwiZGVidWciOmZhbHNlfQ==Node.js snippet
Buffer.from(str, "utf8").toString("base64") is the idiomatic Node.js approach and handles UTF-8 correctly without the TextEncoder workaround needed for browser btoa().
Browser-side only
Encoding runs locally in your browser. Nothing is transmitted.
Frequently Asked Questions
Why does btoa() fail on non-ASCII strings?
btoa() only handles Latin-1 characters. For UTF-8 strings (emojis, accented chars, CJK), you must encode to UTF-8 bytes first. This tool handles that automatically.
What is the correct way to Base64-encode UTF-8 in JavaScript?
Use TextEncoder to get UTF-8 bytes, then encode with btoa: btoa(String.fromCharCode(...new TextEncoder().encode(str))). In Node.js: Buffer.from(str, "utf8").toString("base64").
When should I use URL-safe Base64?
Use URL-safe mode when the encoded string will appear in a URL query parameter, a JWT, a cookie, or a filename. It replaces + with - and / with _ and removes = padding.
Is my data sent to a server?
No. Encoding runs entirely in your browser.
Related Tools
Base64 to JavaScript
Decode Base64 strings in a JavaScript context. Covers atob(), Buffer, and URL-safe Base64 patterns.
Base64 Encoder/Decoder
Encode text to Base64 or decode Base64 to plain text — with URL-safe mode, auto-detect, and size stats.
Text to Base64
Encode plain text to Base64 instantly. Handles UTF-8, emojis, and international characters.
JSON to Base64 Converter
Convert valid JSON into Base64 encoding with syntax validation and copy-ready output.
URL Encoder/Decoder
Encode and decode URL strings with special characters.
JWT Decoder
Decode and inspect JSON Web Tokens (JWT) without validation.

