CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

JavaScript - Base64 Encoding

JavaScript - Base64 Encoding

Encoding is the process of converting text from one data format to another. In computer science terms,encoding is the process of converting a text into a cipher text. Encoding is different from the Encryption process.

Base64 encoding is generally used for encoding binary data, such as images, audio, video, etc. It is a group for binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.

Base64 encoding is used in many applications, such as email via MIME, and storing complex data in XML or JSON.

String to Base64 Encoding using btoa()

A string in base-64 is encoded into Base64-encoded ASCII string using the btoa() function. The syntax for btoa() method is

JAVASCRIPT CODE
btoa(string)

Where, the parameter string is the string to be encoded. The return value is an encoded string.

Example

This is an example program to encode a string using btoa() method.

Preview

Output

Following is the output of the above program.

JAVASCRIPT CODE
The encoded string for "Tutorials point" is VHJ1c3Rpb25zIHBvaW50

You may get different output as the encoded string is different for different strings.

Base64 Decoding using atob()

Decoding is the way which can be used to convert the encoded string back to the original string. atob() is an easy method to decode the Base64 encoded string. The syntax for atob() method is

JAVASCRIPT CODE
atob(encodedString)

Where, the parameter encodedString is to be decoded. The return value is the original string.

Example

This is an example program to decode a string using atob() method.

Preview

Output

Following is the output of the above program.

JAVASCRIPT CODE
The decoded string for "VHV0b3J<p>YWxzIHBvaW50" is Tutorials point&lt;/p&gt;

You may get different output as the decoded string is different for different strings.