Encode and Decode the String object to Base64 and from Base64 with Javascript

Our Own Score

Our mission here is to create two functions under the String object that will help us to encode and decode the String object to Base64 and from Base64. Before we start let us look at what is the purpose of encoding a string to base 64, here is the best answer from a user on stackoverflow regarding this question:

When you have some binary data that you want to ship across a network, you generally don’t do it by just streaming the bits and bytes over the wire in a raw format. Why? because some media are made for streaming text. You never know — some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you’ve entered a special character combination (like how FTP translates line endings).

So to get around this, people encode the binary data into characters. Base64 is one of these types of encodings.

Why 64?
Because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data’s going to end up on the other side of the wire uncorrupted.

If you further read the document on Mozilla website you will find further facts about base64

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

These are the two functions recommend by Mozilla that we can use to encode and decode a string to and from base 64.

In JavaScript there are two functions respectively for decoding and encoding base64 strings:


The atob() function decodes a string of data which has been encoded using base-64 encoding. Conversely, the btoa() function creates a base-64 encoded ASCII string from a “string” of binary data.

But we are not going to use the above two functions this time instead we will use the functions in the Buffer object to do the encoding and decoding process. The two functions above are great and you can use them to do most of the encoding and decoding process but let us look at the Buffer object’s functions this time around.

The game plan is very simple, we will create two functions under the String object’s property, one is toBase64 function which will encode a string to base64, next is fromBase64 function which is use to decode the encoded string back to it’s original value.

//Extend the String object with toBase64() and fromBase64() functions

// to base 64
String.prototype.toBase64 = function() {
	return Buffer.from(this.toString()).toString('base64')
// from base 64
String.prototype.fromBase64 = function() {
	return Buffer.from(this.toString(), 'base64').toString()

Now lets encode a string

console.log('the us dollar stays strong!'.toBase64()); // output 'dGhlIHVzIGRvbGxhciBzdGF5cyBzdHJvbmch'

Next lets do the decoding

console.log('dGhlIHVzIGRvbGxhciBzdGF5cyBzdHJvbmch'.fromBase64()); // outcome 'the us dollar stays strong!'

Which is as per expected!


Leave a Reply

Notify of