Quick Overview
js-base64 is a Base64 implementation for JavaScript. It provides functions for encoding and decoding strings and files using the Base64 algorithm, supporting both browser and Node.js environments. The library is lightweight and easy to use, making it a popular choice for Base64 operations in JavaScript projects.
Pros
- Supports both browser and Node.js environments
- Lightweight and easy to use
- Provides additional utility functions beyond basic encoding/decoding
- Well-maintained with regular updates
Cons
- Limited to Base64 encoding/decoding (no support for other encoding schemes)
- May have performance limitations for very large data sets
- Requires additional setup for use in certain module systems
Code Examples
Encoding a string to Base64:
import { Base64 } from 'js-base64';
const encoded = Base64.encode('Hello, World!');
console.log(encoded); // SGVsbG8sIFdvcmxkIQ==
Decoding a Base64 string:
import { Base64 } from 'js-base64';
const decoded = Base64.decode('SGVsbG8sIFdvcmxkIQ==');
console.log(decoded); // Hello, World!
Encoding and decoding Unicode strings:
import { Base64 } from 'js-base64';
const unicodeString = '你好,世界!';
const encoded = Base64.encode(unicodeString);
console.log(encoded); // 5L2g5aW977yM5LiW55WM77yB
const decoded = Base64.decode(encoded);
console.log(decoded); // 你好,世界!
Getting Started
To use js-base64 in your project, first install it via npm:
npm install js-base64
Then, import and use it in your JavaScript code:
import { Base64 } from 'js-base64';
// Encoding
const encoded = Base64.encode('Your string here');
// Decoding
const decoded = Base64.decode(encoded);
console.log(encoded, decoded);
For browser usage, you can include the library via a CDN:
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.min.js"></script>
<script>
const encoded = Base64.encode('Your string here');
console.log(encoded);
</script>
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
base64.js
Yet another Base64 transcoder.
Install
$ npm install --save js-base64
Usage
In Browser
Locallyâ¦
<script src="base64.js"></script>
⦠or Directly from CDN. In which case you don't even need to install.
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.7.7/base64.min.js"></script>
This good old way loads Base64
in the global context (window
). Though Base64.noConflict()
is made available, you should consider using ES6 Module to avoid tainting window
.
As an ES6 Module
locallyâ¦
import { Base64 } from 'js-base64';
// or if you prefer no Base64 namespace
import { encode, decode } from 'js-base64';
or even remotely.
<script type="module">
// note jsdelivr.net does not automatically minify .mjs
import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.7.7/base64.mjs';
</script>
<script type="module">
// or if you prefer no Base64 namespace
import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.7.7/base64.mjs';
</script>
node.js (commonjs)
const {Base64} = require('js-base64');
Unlike the case above, the global context is no longer modified.
You can also use esm to import
instead of require
.
require=require('esm')(module);
import {Base64} from 'js-base64';
SYNOPSIS
let latin = 'dankogai';
let utf8 = 'å°é£¼å¼¾'
let u8s = new Uint8Array([100,97,110,107,111,103,97,105]);
Base64.encode(latin); // ZGFua29nYWk=
Base64.encode(latin, true); // ZGFua29nYWk skips padding
Base64.encodeURI(latin); // ZGFua29nYWk
Base64.btoa(latin); // ZGFua29nYWk=
Base64.btoa(utf8); // raises exception
Base64.fromUint8Array(u8s); // ZGFua29nYWk=
Base64.fromUint8Array(u8s, true); // ZGFua29nYW which is URI safe
Base64.encode(utf8); // 5bCP6aO85by+
Base64.encode(utf8, true) // 5bCP6aO85by-
Base64.encodeURI(utf8); // 5bCP6aO85by-
Base64.decode( 'ZGFua29nYWk=');// dankogai
Base64.decode( 'ZGFua29nYWk'); // dankogai
Base64.atob( 'ZGFua29nYWk=');// dankogai
Base64.atob( '5bCP6aO85by+');// 'å°Â飼弾' which is nonsense
Base64.toUint8Array('ZGFua29nYWk=');// u8s above
Base64.decode( '5bCP6aO85by+');// å°é£¼å¼¾
// note .decodeURI() is unnecessary since it accepts both flavors
Base64.decode( '5bCP6aO85by-');// å°é£¼å¼¾
Base64.isValid(0); // false: 0 is not string
Base64.isValid(''); // true: a valid Base64-encoded empty byte
Base64.isValid('ZA=='); // true: a valid Base64-encoded 'd'
Base64.isValid('Z A='); // true: whitespaces are okay
Base64.isValid('ZA'); // true: padding ='s can be omitted
Base64.isValid('++'); // true: can be non URL-safe
Base64.isValid('--'); // true: or URL-safe
Base64.isValid('+-'); // false: can't mix both
Built-in Extensions
By default Base64
leaves built-in prototypes untouched. But you can extend them as below.
// you have to explicitly extend String.prototype
Base64.extendString();
// once extended, you can do the following
'dankogai'.toBase64(); // ZGFua29nYWk=
'å°é£¼å¼¾'.toBase64(); // 5bCP6aO85by+
'å°é£¼å¼¾'.toBase64(true); // 5bCP6aO85by-
'å°é£¼å¼¾'.toBase64URI(); // 5bCP6aO85by- ab alias of .toBase64(true)
'å°é£¼å¼¾'.toBase64URL(); // 5bCP6aO85by- an alias of .toBase64URI()
'ZGFua29nYWk='.fromBase64(); // dankogai
'5bCP6aO85by+'.fromBase64(); // å°é£¼å¼¾
'5bCP6aO85by-'.fromBase64(); // å°é£¼å¼¾
'5bCP6aO85by-'.toUint8Array();// u8s above
// you have to explicitly extend Uint8Array.prototype
Base64.extendUint8Array();
// once extended, you can do the following
u8s.toBase64(); // 'ZGFua29nYWk='
u8s.toBase64URI(); // 'ZGFua29nYWk'
u8s.toBase64URL(); // 'ZGFua29nYWk' an alias of .toBase64URI()
// extend all at once
Base64.extendBuiltins()
.decode()
vs .atob
(and .encode()
vs btoa()
)
Suppose you have:
var pngBase64 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
Which is a Base64-encoded 1x1 transparent PNG, DO NOT USE Base64.decode(pngBase64)
. Â Use Base64.atob(pngBase64)
instead. Â Base64.decode()
decodes to UTF-8 string while Base64.atob()
decodes to bytes, which is compatible to browser built-in atob()
(Which is absent in node.js). Â The same rule applies to the opposite direction.
Or even better, Base64.toUint8Array(pngBase64)
.
Brief History
- Since version 3.3 it is written in TypeScript. Now
base64.mjs
is compiled frombase64.ts
thenbase64.js
is generated frombase64.mjs
. - Since version 3.7
base64.js
is ES5-compatible again (hence IE11-compatible). - Since 3.0
js-base64
switch to ES2015 module so it is no longer compatible with legacy browsers like IE (see above)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot