Войдите или зарегистрируйтесь
Вы сможете писать комментарии и посты, ставить лайки и другое
Поиск
Тёмная тема

Misleading naming in JavaScript: atob() and btoa()

недавно
JavaScript has two globally available metods for working with Base64: atob() and btoa(). Their names clearly look like they were borrowed from older languages. In C, for instance, the standard library includes functions like atoi and atof:

#include <stdlib.h>

char str[] = "123";
int num = atoi(str); // 123

atoi means ASCII to integer, and atof means ASCII to float (though in reality it returns a double).

So what do you think the atob function does in JavaScript? ASCII to Base64? In other words, converting a regular string into a Base64 string? Nope! It does the exact opposite: it converts a Base64 string into a "regular" string. And btoa, in turn, converts a regular string into Base64!

console.log(btoa('Famabara')); // 'RmFtYWJhcmE='
console.log(atob('RmFtYWJhcmE')); // 'Famabara'

Who thought it was a good idea to swap the names like that? Love JS.
+1
21

Preformatted text

1 г. назад
Famabara supports preformatted text now. You can use it inside posts and comments.

Just write some text between triple backticks "```".
Backticks must be placed in the beginning of a new line.

const years = [2024, 2030, 2050];
for (const year of years) {
  console.log(year);
};
It can be useful when showing some code - all spaces are saved:
<div>
  <div>
    Some text
  </div>
  <div>
    Other text
  </div>
</div>
+2
83