Table of contents
1.
Introduction
2.
What is `atob()` in JavaScript?  
2.1.
How Does `atob()` Work?  
2.2.
When to Use `atob()`  
2.3.
Important Notes  
3.
Syntax of Window atob() in JavaScript
4.
Parameters of Window atob() in JavaScript
5.
Return Value of Window atob() in JavaScript
6.
Exceptions of Window atob() in JavaScript
6.1.
Example of Exception Handling
7.
Example
8.
Frequently Asked Questions
8.1.
What is the difference between btoa() and atob() in JavaScript?
8.2.
Can atob() decode a string that is not Base64-encoded?
8.3.
Does atob() support Unicode characters?
9.
Conclusion
Last Updated: Feb 19, 2025
Medium

Window atob() in JavaScript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The atob() method in JavaScript is used to decode a Base64-encoded string. It converts encoded data back into its original ASCII format, making it useful for handling encoded content such as images, files, or authentication tokens. However, it works only with properly encoded Base64 strings.

Window atob() in JavaScript

In this article, you will learn the syntax, usage, and examples of the atob() method, along with best practices for decoding Base64 strings in JavaScript.

What is `atob()` in JavaScript?  

The `atob()` function is a built-in JavaScript method that stands for "ASCII to Binary." It is used to decode a string of data that has been encoded using Base64 encoding. Base64 is a way to represent binary data (like images or files) in a text format, which makes it easier to transmit over systems designed to handle text, such as emails or URLs.  

In simpler terms, `atob()` takes a Base64-encoded string as input and returns the original binary data in the form of a string. This is particularly useful when you receive encoded data from an API or need to process encoded strings in your application.  

How Does `atob()` Work?  

The `atob()` function works by taking a Base64-encoded string and converting it back to its original form. For example, if you have a Base64-encoded string like `"SGVsbG8gV29ybGQ="`, it will decode it to `"Hello World"`.  

For example: 

// Example of using atob() to decode a Base64 string
const encodedString = "SGVsbG8gV29ybGQ="; // This is the Base64-encoded string
const decodedString = atob(encodedString); // Decoding the string

console.log(decodedString); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

"Hello World"

 

In this Code: 

1. `encodedString`: This is the Base64-encoded string we want to decode. In this case, `"SGVsbG8gV29ybGQ="` is the encoded version of `"Hello World"`.  
 

2. `atob(encodedString)`: The `atob()` function takes the encoded string as input and decodes it.  
 

3. `decodedString`: This stores the decoded result, which is `"Hello World"`.  
 

4. `console.log(decodedString)`: This prints the decoded string to the console.  

When to Use `atob()`  

  • When you receive Base64-encoded data from an API or server.  
     
  • When you need to process encoded strings, such as decoding images or files sent over the web.  
     
  • When working with data URLs, which often use Base64 encoding.  

Important Notes  

  • `atob()` only works with valid Base64-encoded strings. If you pass an invalid string, it will throw an error.  
     
  • The decoded result is always a string. If the original data was binary (like an image), you’ll need additional steps to convert it back to its original format.  

Syntax of Window atob() in JavaScript

The syntax of the atob() function is straightforward and easy to use. It takes a single argument, which is the Base64-encoded string, and returns the decoded string.

let decodedString = window.atob(encodedString);

 

Explanation:

  • window.atob() is a built-in function.
     
  • encodedString is the Base64-encoded string that needs to be decoded.
     
  • decodedString stores the output after decoding.

Parameters of Window atob() in JavaScript

The atob() function accepts a single parameter:

ParameterDescription
encodedStringA valid Base64-encoded string that needs to be decoded.

 

Important Notes:

  • The input string must be a valid Base64-encoded string.
     
  • If the input contains characters that are not part of the Base64 character set, an error will be thrown.

Return Value of Window atob() in JavaScript

The function returns a decoded string from the given Base64-encoded input.

Return TypeDescription
StringThe original string before encoding.

Exceptions of Window atob() in JavaScript

The atob() function may throw an exception in the following cases:

ExceptionReason
DOMExceptionThrown when the input string contains characters that are not valid Base64 characters.

Example of Exception Handling

try {
    let decodedString = atob("InvalidBase64#");
    console.log(decodedString);
} catch (error) {
    console.error("Error: Invalid Base64 input");
}
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Error: Invalid Base64 input

Example

Let's look at a complete example of encoding a string using btoa() and then decoding it using atob().

// Encoding a string to Base64
let originalString = "Hello, Coding Ninjas!";
let encodedString = btoa(originalString);
console.log("Encoded String:", encodedString);

// Decoding the Base64 string back to normal text
let decodedString = atob(encodedString);
console.log("Decoded String:", decodedString);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

Encoded String: SGVsbG8sIENvZGluZyBOaW5qYXMh
Decoded String: Hello, Coding Ninjas!

Frequently Asked Questions

What is the difference between btoa() and atob() in JavaScript?

The btoa() function encodes a string into Base64 format, while atob() decodes a Base64-encoded string back to its original form.

Can atob() decode a string that is not Base64-encoded?

No, if the input string is not a valid Base64-encoded string, atob() will throw a DOMException error.

Does atob() support Unicode characters?

No, atob() does not support direct decoding of Unicode characters. You need to use TextEncoder and TextDecoder for handling Unicode text properly.

Conclusion

In this article, we explored the atob() method in JavaScript, which decodes a Base64-encoded string back to its original format. It is commonly used for handling encoded data in web applications. Understanding atob() helps developers work with encoded content efficiently, especially in scenarios like data transmission and authentication.

Live masterclass