Search code examples
javascriptbase64ascii

atob() and btoa() use cases


What are the use cases for atob() and btoa() other than when trying to prevent communication problems in information transfer?

I've researched this online, but I didn't find anything other than information transfer communication problems. This is the resource that I found: atob() and btoa()


Solution

  • See what is base 64 encoding used for.

    In particular, on the web, base-64 encoding is useful for contexts where binary data is forbidden, which includes much of HTML. For example, if you want to inline a PNG image file into a URL, for use in an <img src> for example, you can write data:image/png;base64,... where ... is the base-64 encoding of the PNG file. btoa is useful for generating such URLs, like so:

    const url = "data:image/png;base64," + btoa(pngContent);