Encode and Decode in Javascript (Base64)

Abhishek Mallick
1 min readMay 11, 2020
Base64 Encoding Decoding

Let us start with a simple conversion which will make it understandable what is Base64 meant by using this link to convert the encoded message below to understandable content.

Q29uZ3JhdHVsYXRpb25zISBZb3UgZmlndXJlZCBvdXQgdGhlIG1lYW5pbmcgb2YgdGhlIGVuY29kZWQgZGF0YSBpbiBCYXNlNjQu

Where to Use?

It is used to transfer content-based messages over the Internet.

Why Use it?

For example, mail systems cannot deal with binary data because they expect ASCII (textual) data. So if you want to transfer an image or another file, it will get corrupted because of the way it deals with the data.

Now let us try to do some Base64 Encoding (btoa) and some Base64 Decoding (atob) using Javascript:

Base64 Encoding in Javascript

Try this piece of code:

Base64 Decoding in Javascript

Try this piece of code:

Please Note:

Base64 encoding is NOT a way of encrypting, nor a way of compacting data. A base64 encoded piece of data is 1.333… times bigger than the original data piece. It is only a way to be sure that no data is lost or modified during the transfer.

Hope it is helpful for you. Please tell me if you have any query or want to share something. Also please appreciate if you like it.

--

--

Abhishek Mallick

Innovator, Visionary, Lead, Researcher and Creator. I work on multiple technologies with multi-level of complexity.