btoa
Encodes a string to Base64.
Available since: Web API
Syntax
btoa(str)Parameters
| Parameter | Type | Description |
|---|---|---|
| str | string | A string of Latin characters (codes 0–255). |
Return value
stringA Base64-encoded string.
Examples
Encoding to Base64:
console.log(btoa('Hello'));Output:
SGVsbG8=Cyrillic and emoji must be turned into bytes first:
const bytes = new TextEncoder().encode('Привет');
const base64 = btoa(String.fromCharCode(...bytes));
console.log(base64);Output:
0J/RgNC40LLQtdGCNotes
The name means binary to ASCII. Base64 is not encryption: anyone can decode the string back with atob(). Without TextEncoder non-Latin characters throw an InvalidCharacterError.
See also
- atob — Decodes a Base64 string.
- encodeURIComponent — Encodes text for safely putting it into part of a URL.
Related recipes
In the tutorial
- String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.