website/content/blog/js.md

66 lines
2.6 KiB
Markdown
Raw Normal View History

+++
title = "Why I hate JavaScript"
date = 2023-01-20
draft = true
[taxonomies]
tags = []
+++
Why do people use JavaScript? Because it's a great language or because it's the only one ready-to-use in any web browser?
Note that several of these arguments also apply to PHP, another _de facto_ application-specific (web server) language, that I think is mostly used because of technical debt.
This article is about JavaScript for web browsers, not NodeJS or TypeScript.
## TL; DR
JavaScript combines all the problems of both high and low level languages.
## Lack of features
JavaScript is obviously not made for being used in webpages.
* need jQuery
* no native HTML escaping function
* no native basic templating system (need `String.replaceAll("{{foo}}", foo)`)
* no easy-to-use bytes array (like `Bytes` in Python or `Vec<u8>` in Rust), but many different bytes arrays with weird interfaces
* no native cryptographic tools if you aren't using HTTPS and satisfying other strange "security" requirements (you often end up inefficiently implementing SHA2 or AES in JS, while the browser natively uses OpenSSL)
* no native date/time string formatting
* When defining an anonymous function (such as a callback) you can't reliably move variables to it.
## What the fuck
JavaScript doesn't respect anything.
* You never know whether you're copying or borrowing (a table or an object), and how deeply.
* Python suffers the same problem.
* Time is expressed in milliseconds, while the convention is seconds everywhere else.
* `"1" == true && "2" != true && "2" != false` is true
* ```js
if("2" == true)
console.log("I am not executed.");
if("2")
console.log("I am executed!");
```
* Hence `==`, `===` and `if` have three distinct strictness levels.
* `&&` is not commutative: `(true && "2") === "2" && ("2" && true) === true` is true
* Operators are inconsistent: `"1"+1 === "11" && "1"*2 === 2` are true
* ```js
var n = "1";
n += 1;
// n is now "11"
n ++;
// n should be "111"?
// but n is now 12
```
* `(-1)%2 == -1` while most of the time the convention is to give the least positive congruent.
* Naming is incoherent. `XMLHttpRequest` is an example mixing all-caps and first-letter-cap conventions for initialisms. It should have been either `XmlHttpRequest` or `XMLHTTPRequest`.
* Is an array really an array, or an object with _some_ of the array's properties? Lua has the same problem.
```js
var foo = [];
foo["bar"] = 42;
console.log(foo["bar"]); // foo["bar"] exists.
console.log(foo.length); // 0
```
* `document.getElementsByClassName` does not return an array: its return type supports indexing but not for/in loops.