btoa/atob(string)
These functions convert between binary strings and base64-encoded ASCII strings, useful for data encoding and decoding operations.
btoa() - Binary to ASCII
Encodes a string of data to base64.
Syntax
Parameters
content
: String - The string data to be encoded to base64
Return Value
Returns a base64-encoded ASCII string.
atob() - ASCII to Binary
Decodes a base64-encoded string back to its original form.
Syntax
Parameters
content
: String - The base64-encoded string to be decoded
Return Value
Returns the decoded string.
Use Cases
These functions are particularly useful for:
Encoding binary data to be transmitted as text
Working with data URLs
Storing binary data in text-based formats
Handling image data in data URI format
Basic data obfuscation (not for security purposes)
Examples
Data URI Creation
Simple Text Encoding/Decoding
Working with APIs that Return Base64
Notes
These functions work with strings, not arbitrary binary data
For full Unicode support, you may need additional handling
Not suitable for encrypting sensitive data
Very large strings may cause performance issues
If the input to
atob()
is not a valid base64 string, it will throw an error
Last updated
Was this helpful?