Skip to content

Commit

Permalink
Fix base64 code example (mdn#32518)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwyx authored Mar 3, 2024
1 parent 0eaece3 commit 45421b1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions files/en-us/glossary/base64/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function base64ToBytes(base64) {
}

function bytesToBase64(bytes) {
const binString = String.fromCodePoint(...bytes);
const binString = Array.from(bytes, (byte) =>
String.fromCodePoint(byte),
).join("");
return btoa(binString);
}

Expand All @@ -65,7 +67,7 @@ new TextDecoder().decode(base64ToBytes("YSDEgCDwkICAIOaWhyDwn6aE")); // "a Ā

The `bytesToBase64` and `base64ToBytes` functions in the previous section can be used directly to convert between Base64 strings and [`Uint8Array`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)s.

Alternatively, asynchronous conversion between base64 data URLs is possible natively within the web platform via the [`FileReader`](/en-US/docs/Web/API/FileReader) and [`fetch`](/en-US/docs/Web/API/Fetch_API) APIs:
For better performance, asynchronous conversion between base64 data URLs is possible natively within the web platform via the [`FileReader`](/en-US/docs/Web/API/FileReader) and [`fetch`](/en-US/docs/Web/API/Fetch_API) APIs:

```js
async function bytesToBase64DataUrl(bytes, type = "application/octet-stream") {
Expand Down

0 comments on commit 45421b1

Please sign in to comment.