In Javascript, window.atob()
method decodes a base64 string and window.btoa()
method encodes a string
into base64.
Then why weren't they named like base64Decode()
and base64Encode()
? atob()
and btoa()
don't make sense because they're not semantic at all.
I want to know the reason.
The atob() function decodes a string of data which has been encoded using Base64 encoding. You can use the btoa() method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob() method to decode the data again.
btoa() The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).
btoa(): accepts a string where each character represents an 8bit byte. If you pass a string containing characters that cannot be represented in 8 bits, it will probably break. Probably that's why btoa is deprecated.
js. If you're trying to use btoa in Node, you'll notice that it is deprecated. It'll suggest you to use the Buffer class instead.
The atob()
and btoa()
methods allow authors to transform content to and from the base64 encoding.
In these APIs, for mnemonic purposes, the "b" can be considered to stand for "binary", and the "a" for "ASCII". In practice, though, for primarily historical reasons, both the input and output of these functions are Unicode strings.
From : http://www.w3.org/TR/html/webappapis.html#atob
I know this is old, but it recently came up on Twitter, and I thought I'd share it as it is authoritative.
Me:
@BrendanEich did you pick those names?
Him:
Old Unix names, hard to find man pages rn but see https://www.unix.com/man-page/minix/1/btoa/ …. The names carried over from Unix into the Netscape codebase. I reflected them into JS in a big hurry in 1995 (after the ten days in May but soon).
In case the Minix link breaks, here's the man page content:
BTOA(1) BTOA(1) NAME btoa - binary to ascii conversion SYNOPSIS btoa [-adhor] [infile] [outfile] OPTIONS -a Decode, rather than encode, the file -d Extracts repair file from diagnosis file -h Help menu is displayed giving the options -o The obsolete algorithm is used for backward compatibility -r Repair a damaged file EXAMPLES btoa <a.out >a.btoa # Convert a.out to ASCII btoa -a <a.btoa >a.out # Reverse the above DESCRIPTION Btoa is a filter that converts a binary file to ascii for transmission over a telephone line. If two file names are provided, the first in used for input and the second for out- put. If only one is provided, it is used as the input file. The program is a function- ally similar alternative to uue/uud, but the encoding is completely different. Since both of these are widely used, both have been provided with MINIX. The file is expanded about 25 percent in the process. SEE ALSO uue(1), uud(1).
Source: Brendan Eich, the creator of JavaScript. https://twitter.com/BrendanEich/status/998618208725684224
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With