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.

