Thanks to visit codestin.com
Credit goes to coreui.io

How to decode a base64 string in JavaScript

Decoding base64 strings is essential for processing API responses, handling file data, reading encoded content, and implementing features like data import or content restoration in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented base64 decoding extensively in components like data processors, file viewers, and API integrations where converting encoded data back to readable format is crucial for functionality. From my extensive expertise, the most straightforward and browser-native solution is using the built-in atob() function, which provides standard base64 decoding. This approach is efficient, widely supported, and specifically designed for ASCII-to-binary conversion in web environments.

Use the atob() function to decode a base64-encoded string back to its original format.

const encoded = 'SGVsbG8gV29ybGQ='
const decoded = atob(encoded)
// Result: 'Hello World'

The atob() function (ASCII-to-binary) converts a base64-encoded string back to its original text representation. In this example, atob('SGVsbG8gV29ybGQ=') produces ‘Hello World’, which is the original string before encoding. The function expects a valid base64 string and will throw an error if the input contains invalid characters. Like btoa(), this function works with strings containing characters in the 0-255 range, so additional steps may be needed for Unicode content.

Best Practice Note:

This is the same approach we use in CoreUI components for processing API data, handling file content, and restoring encoded information across our component ecosystem. For Unicode strings that were encoded with special handling, use: decodeURIComponent(atob(encoded).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('')). In Node.js, use Buffer.from(encoded, 'base64').toString(). Always wrap atob() in try-catch blocks to handle malformed base64 input gracefully.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Achieve Perfectly Rounded Corners in CSS
How to Achieve Perfectly Rounded Corners in CSS

How to check if an element is visible in JavaScript
How to check if an element is visible in JavaScript

JavaScript Operator Precedence and Associativity: A Developer's Complete Guide
JavaScript Operator Precedence and Associativity: A Developer's Complete Guide

How to Open All Links in New Tab Using JavaScript
How to Open All Links in New Tab Using JavaScript

Answers by CoreUI Core Team