2.6 KiB
2.6 KiB
+++ 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
Bytesin Python orVec<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" != falseis true-
if("2" == true) console.log("I am not executed."); if("2") console.log("I am executed!");- Hence
==,===andifhave three distinct strictness levels.
- Hence
&&is not commutative:(true && "2") === "2" && ("2" && true) === trueis true- Operators are inconsistent:
"1"+1 === "11" && "1"*2 === 2are true -
var n = "1"; n += 1; // n is now "11" n ++; // n should be "111"? // but n is now 12 (-1)%2 == -1while most of the time the convention is to give the least positive congruent.- Naming is incoherent.
XMLHttpRequestis an example mixing all-caps and first-letter-cap conventions for initialisms. It should have been eitherXmlHttpRequestorXMLHTTPRequest. - Is an array really an array, or an object with some of the array's properties? Lua has the same problem.
var foo = []; foo["bar"] = 42; console.log(foo["bar"]); // foo["bar"] exists. console.log(foo.length); // 0 document.getElementsByClassNamedoes not return an array: its return type supports indexing but not for/in loops.