DevToolsForYou

Base64 Encoder and Decoder in JavaScript / Node.js — Code Examples

Base64 Encoder and Decoder in JavaScript / Node.jsUse the online tool →

Base64 encoding converts binary or text data into a 64-character ASCII alphabet so it can be safely transmitted over text-only channels. Here is how to encode and decode Base64 in each language using only the standard library.

In browsers use btoa() and atob(). In Node.js use Buffer. For Unicode safety in the browser, encode via TextEncoder first.

Browser
// Encode
const encoded = btoa("Hello, world!");
console.log(encoded); // SGVsbG8sIHdvcmxkIQ==

// Decode
const decoded = atob("SGVsbG8sIHdvcmxkIQ==");
console.log(decoded); // Hello, world!

// Unicode-safe encode (handles emoji, non-ASCII)
function encodeUnicode(str) {
  return btoa(
    String.fromCharCode(...new TextEncoder().encode(str))
  );
}

// Unicode-safe decode
function decodeUnicode(b64) {
  return new TextDecoder().decode(
    Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))
  );
}
Node.js
// Encode
const encoded = Buffer.from("Hello, world!").toString("base64");
console.log(encoded); // SGVsbG8sIHdvcmxkIQ==

// Decode
const decoded = Buffer.from("SGVsbG8sIHdvcmxkIQ==", "base64").toString("utf8");
console.log(decoded); // Hello, world!

// URL-safe Base64 (replaces + with - and / with _)
const urlSafe = Buffer.from("Hello, world!").toString("base64url");
Notes & gotchas
  • btoa() in browsers accepts Latin-1 only — wrap with TextEncoder for Unicode input.
  • Node.js Buffer handles Unicode correctly without extra steps.
  • Use "base64url" encoding for JWTs and URL-safe contexts.
Try it in your browser

Need to base64 encode/decode without writing code? The Base64 Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Base64 Encode/Decode
Base64 Encoder and Decoder in other languages