JavaScript Base64 Encoder & Decoder
Encode and decode Base64 strings for JavaScript projects. Test your Base64 operations instantly, then use the code examples for browser (btoa/atob) and Node.js (Buffer) implementations. 100% client-side — your data stays in your browser.
How to encode Base64 in JavaScript
In the browser, use btoa() to encode: const encoded = btoa('Hello World'). For Node.js, use Buffer: const encoded = Buffer.from('Hello World').toString('base64'). Important: btoa() only handles Latin-1 characters. For Unicode strings, first encode to UTF-8: const encoded = btoa(unescape(encodeURIComponent(unicodeString))). Or in modern environments: const encoded = btoa(new TextEncoder().encode(str).reduce((s, b) => s + String.fromCharCode(b), '')).
How to decode Base64 in JavaScript
Browser decoding: const decoded = atob(encodedString). Node.js: const decoded = Buffer.from(encodedString, 'base64').toString('utf-8'). For Base64URL (used in JWTs and URLs), replace - with + and _ with / before decoding, or use a library. Common error: 'InvalidCharacterError: The string to be decoded is not correctly encoded' — this means your Base64 string has invalid characters or incorrect padding. Use this tool to verify your Base64 string is valid before decoding in code.
Base64 in JavaScript: browser vs Node.js
Browser JavaScript uses btoa()/atob() (Binary to ASCII and ASCII to Binary). Node.js uses Buffer.from(str, 'base64') and buffer.toString('base64'). In Deno and Bun, both APIs are available. For isomorphic code that works everywhere, consider the base64-js npm package or a polyfill. The Fetch API also supports Base64 via: await (await fetch('data:;base64,' + encoded)).text(). For file uploads, FileReader.readAsDataURL() returns a Base64 data URL automatically.
Frequently Asked Questions
What is the difference between btoa() and Buffer.from().toString('base64')?
btoa() is a browser API that encodes Latin-1 strings to Base64. Buffer.from().toString('base64') is Node.js-specific and handles any encoding including UTF-8. btoa() throws an error on non-Latin-1 characters; Buffer handles Unicode natively.
How do I Base64 encode Unicode strings in JavaScript?
Use TextEncoder: btoa(String.fromCharCode(...new TextEncoder().encode(str))). In Node.js: Buffer.from(str, 'utf-8').toString('base64'). The key is encoding the string to UTF-8 bytes first, then Base64 encoding those bytes.
How do I decode a Base64 data URL in JavaScript?
Split the data URL to get the Base64 part: const base64 = dataUrl.split(',')[1]. Then decode: atob(base64) for text, or fetch(dataUrl).then(r => r.blob()) for binary data like images.
Related Convert Tools
GraphQL to TypeScript
Convert GraphQL SDL schemas to TypeScript interfaces, types, enums, unions, and operations
TypeScript to JavaScript
Convert TypeScript to JavaScript — strip types, interfaces, enums, generics, and access modifiers to get clean JS output
JSON to SQL Converter
Convert JSON arrays to SQL CREATE TABLE and INSERT statements for PostgreSQL, MySQL, and SQLite with automatic type inference
SQL to TypeScript/Prisma/Drizzle
Convert SQL CREATE TABLE statements to TypeScript interfaces, Prisma schema, and Drizzle ORM definitions for PostgreSQL, MySQL, and SQLite