WebToolsPlanet

JavaScript to Base64

Encode JavaScript strings to Base64. Covers btoa(), Buffer, URL-safe mode, and common JS encoding patterns.

  • Input stays on your device

Last updated:

Client-Side Processing
Input Data Stays on Device
Instant Local Execution

Find this tool useful? Support the project to keep it free!

Buy me a coffee

What 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

1

Type or paste the JavaScript string you want to encode

2

Click "Encode to Base64" to generate the encoded output

3

Enable URL-safe mode if the result will appear in a URL or JWT

4

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.

JavaScript string
{"env":"production","debug":false}
Base64 output
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.