Skip to content

base62/base62.js

Base62.js

npm version CI npm downloads license

A JavaScript Base62 encode/decoder.

Base62 encoding converts numbers to ASCII strings (0-9, a-z and A-Z) and vice versa, which typically results in comparatively short strings. Such identifiers also tend to be more readily identifiable by humans.

  • 999 -> "g7"
  • 9999 -> "2Bh"
  • 238327 -> "ZZZ"

Installation

npm install base62

Usage

var base62 = require("base62");

base62.encode(999);  // "g7"
base62.decode("g7"); // 999

Both encode and decode support BigInt for values beyond Number.MAX_SAFE_INTEGER:

base62.encode(9007199254740993n);                // "FfGNdXsE9"
base62.decode("FfGNdXsE9", { bigint: true });    // 9007199254740993n

You can also use a custom character set:

var base62 = require("base62");

base62.setCharacterSet("~9876543210ABCDEFGHIJKLMNOPQRSTU$#@%!abcdefghijklmnopqrstuvw-=");

base62.encode(999);  // "F3"
base62.decode("F3"); // 999

setCharacterSet expects a string containing exactly 62 unique characters.

Development

Source code is hosted on GitHub. Please report issues or feature requests in GitHub Issues.

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it.
  • Send a pull request.

License

MIT. Copyright (c) 2026 Andrew Nesbitt. See LICENSE for details.