Developer Documentation
JavaScript
Updated September 19, 2026

Base64 Encoding and Decoding in JavaScript

Encode and decode Base64 safely, including UTF-8 text and binary data in JavaScript.

Base64 converts binary data to printable ASCII text. It is useful for transport and embedding, but it is an encoding format, not encryption.

Quick Start#

The following example uses the standard JavaScript API or a widely used package for this task.

Browser quick start#

JavaScript
1const text = "Hello, ToolPorta 👋";2const bytes = new TextEncoder().encode(text);3const binary = Array.from(bytes, b => String.fromCharCode(b)).join("");4const encoded = btoa(binary);56const decodedBytes = Uint8Array.from(atob(encoded), c => c.charCodeAt());7const decoded = new TextDecoder().decode(decodedBytes);89console.log(encoded);10console.log(decoded);

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Node.js with Buffer#

JavaScript
1const encoded = Buffer.from("Hello ToolPorta", "utf8").toString("base64");2const decoded = Buffer.from(encoded, "base64").toString("utf8");34console.log({ encoded, decoded });

Important Notes#

Do not call btoa() directly on arbitrary Unicode strings; encode them to bytes first.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Base64 in other languages: