Base64 encoding/decoding in pure JavaScript for binary data
npx @tessl/cli install tessl/npm-base64-js@1.5.0base64-js provides a pure JavaScript implementation for base64 encoding and decoding of binary data. It offers cross-platform compatibility for Node.js and browser environments, supporting both standard and URL-safe base64 encoding variants.
npm install base64-jsconst base64js = require('base64-js');For ES modules:
import * as base64js from 'base64-js';For TypeScript with full type safety:
import { byteLength, toByteArray, fromByteArray } from 'base64-js';Individual imports:
const { byteLength, toByteArray, fromByteArray } = require('base64-js');const base64js = require('base64-js');
// Convert binary data to base64 string
const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
const base64String = base64js.fromByteArray(bytes);
console.log(base64String); // "SGVsbG8="
// Convert base64 string back to binary data
const decodedBytes = base64js.toByteArray(base64String);
console.log(decodedBytes); // Uint8Array [72, 101, 108, 108, 111]
// Calculate byte length of base64 string
const length = base64js.byteLength(base64String);
console.log(length); // 5Calculates the byte length of a base64 encoded string without decoding it.
/**
* Calculate the byte length of a base64 encoded string
* @param {string} b64 - Base64 encoded string
* @returns {number} The byte length of the decoded data
* @throws {Error} If string length is not a multiple of 4
*/
function byteLength(b64);Usage Example:
const base64js = require('base64-js');
const encoded = "SGVsbG8gV29ybGQ="; // "Hello World"
const length = base64js.byteLength(encoded);
console.log(length); // 11Converts a base64 encoded string to a byte array. Supports both standard base64 and URL-safe base64 variants.
/**
* Convert base64 string to byte array
* @param {string} b64 - Base64 encoded string (supports URL-safe variants)
* @returns {Uint8Array|Array} Decoded binary data (Uint8Array in modern environments, Array in legacy browsers)
* @throws {Error} If string length is not a multiple of 4
*/
function toByteArray(b64);Features:
A-Z, a-z, 0-9, +, /- (instead of +) and _ (instead of /)= charactersUint8Array in modern environments, Array in legacy browsers without Uint8Array supportUsage Examples:
const base64js = require('base64-js');
// Standard base64
const standard = "SGVsbG8=";
const bytes1 = base64js.toByteArray(standard);
// URL-safe base64
const urlSafe = "SGVsbG8-"; // Using - instead of +
const bytes2 = base64js.toByteArray(urlSafe);
// Both decode to the same result: [72, 101, 108, 108, 111]Converts a byte array to a base64 encoded string with proper padding.
/**
* Convert byte array to base64 string
* @param {Uint8Array|Array<number>} uint8 - Binary data to encode (accepts both Uint8Array and regular arrays)
* @returns {string} Base64 encoded string with proper padding
*/
function fromByteArray(uint8);Features:
Uint8Array or regular Array<number> of bytes (0-255 range)= paddingUsage Examples:
const base64js = require('base64-js');
// From Uint8Array
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
const encoded1 = base64js.fromByteArray(bytes);
// From regular array
const byteArray = [72, 101, 108, 108, 111];
const encoded2 = base64js.fromByteArray(byteArray);
// Both produce: "SGVsbG8="
// Large data handling
const largeData = new Uint8Array(100000).fill(42);
const encodedLarge = base64js.fromByteArray(largeData);All functions throw Error if the input base64 string has invalid length:
const base64js = require('base64-js');
try {
// Invalid base64 - length not multiple of 4
const result = base64js.toByteArray("SGVsbG8"); // Missing padding
} catch (error) {
console.error(error.message); // "Invalid string. Length must be a multiple of 4"
}
// The same error is thrown by byteLength for invalid strings
try {
const length = base64js.byteLength("SGVsbG8");
} catch (error) {
console.error(error.message); // "Invalid string. Length must be a multiple of 4"
}Uint8ArrayUint8Array for optimal performanceArray when Uint8Array is unavailable- and _ characters per RFC 4648 Section 5