yujiri.xyz

Software

Javascript review

I find it bizarre that people choose to use Javascript when they have other options. I will attempt a comprehensive, neutral analysis of it's quality as a language as I do for other languages.

Best comparison: Python review

Dynamic typing up to 11

I am a fierce opponent of dynamic typing, and have written about benefits of static typing besides the obvious. Javascript defenders will likely point to the existence of TypeScript, but a bolted-on type system will never be as good as a built-in one (argued in the linked article):

Subtle benefits of static typing

But Javascript is even worse here than other dynamic languages. In others, like Python, most things that you'd want to be compile-time errors are still run-time errors. But in Javascript they're often silent failures. For example, indexing an array out of bounds or accessing a nonexistent property of an object gives you `undefined`. Good luck debugging that.

And compounding the confusion: `undefined` *is* a valid value in general!

Even a function parameter just gets `undefined` if it's not passed. All arguments are optional; you *can't* define a function that requires you to pass it a parameter. Let that sink in for a minute.

You also don't get an error when passing too many arguments to a function.

Even indexing a non-array and non-object isn't an error:

Pretty much the only type errors are to index null or undefined or call something that isn't a function.

Let's have fun with some more things that should be errors, but aren't:

Most of these are unintuitive footguns if not complete nonsense. An operation that doesn't involve numbers should never come out as `NaN`; that's not what `NaN` means.

In general, things that are almost certainly mistakes should raise exceptions, not silently return a nonsensical value.

Arrays are objects?

Javascript arrays aren't really arrays, but objects. I don't just say this because `typeof [] === 'object'`; there are a lot of harmful ways in which the language doesn't seem to think of them as an actual sequence type. One is that you can assign past the end of an array and you just get "empty items" inbetween:

See what I mean? It's like you're just assigning keys in an object, and array indices don't have any special meaning (though they do print sensibly).

And those empty items *aren't the same as undefined* (if they were, that would imply a deeper difference between arrays and objects than Javascript seems to want to admit). Or they are, but they're not. Check this out:

It's like the holy trinity of `undefined`!

This is because arrays have a `length` attribute that stores the number of elements they supposedly have. So when you assign to an index, it changes the length, and then when you look at the array all the slots inbetween that don't exist as keys in the array are presented as these "empty items". `delete` is meant for removing a key from an object, so when used on an array, it only deletes the key and doesn't collapse the others or modify the `length` attribute, so it just leaves an empty slot behind. Pretty bad newb trap.

Array.splice is a bad API; it is both for deleting and for inserting elements. There should be separate methods for each.

Lack of syntactic support for arrays

Because of the way Javascript treats arrays as objects, it supports neither negative indices nor slicing. Just compare the readability difference (Python vs Javascript):

Objects can't compare for equality

`==` on objects (including arrays) compares for identity, not equality. If you want to test whether two objects are equal, you have to iterate over their keys.

In a language that has `==` and `===`, you would think `==` would compare by value for objects, and `===` would compare identity. But no, in the one case where having two equality operators might actually make sense, they do the same thing.

Object constructors for primitive types

There's literally no point to the existence of these; it's just a newb trap that exists because of how constructors work in Javascript.

And this one is even funnier:

Because objects are always true.

Error handling

Javascipt uses exceptions like other dynamic languages, but it's lacking over Python and Ruby in that it doesn't support catching only specific types of exceptions. `catch` always catches everything and you have to manually check and reraise if you only wanted to catch some kinds. And like the others, it catches name errors. Ugh.

Why do all the dynamic languages catch name errors by default?

It does give good stack traces, at least.

`var` wart

The `var` keyword is an unfortunate historical wart. `let` and `const` are the correct ways to declare variables in modern Javascript; `var` is function-scoped instead of block-scoped, and has the bizarre behavior of applying to references *before* itself: if you have naked `foo = 5` and then later in the function `var foo;` there is no error and the value of `foo` will be `5` right after `var foo;`.

Arrow functions

Most dynamic languages have `map`, `filter`, `reduce`, and lambdas, but Javascript leads the others in the functional programming department with arrow functions:

Arrow functions

They are more concise than the `function` keyword, and the syntax is intuitive too; it *looks* like you're taking the parameter list and doing something with it. Python has lambdas and in-function `def`, but lambdas are limited to just a `return` statement and `def` doesn't handle scoping the same way arrow functions do.

this article on Pylint shows an example of the difference where you would want the arrow function behavior.

Ecosystem

The NPM ecosystem is plagued worse than any other package ecosystem with overdependency. You can barely install anything without populating your `node_modules` with at least a hundred directories. This means it's impossible to audit your dependencies in any significant Javascript project.

Dependencies and maintainers by Drew DeVault

A lot of the dependencies are nonsense packages, too, which provide a single function of often just *1* line.

This article is a good read on the situation

Ecosystems of other languages don't have this problem. Even Django, the giant all-the-features Python web framework, has only *3* dependencies last I checked, including indirect.

Proxied content from gemini://yujiri.xyz/software/javascript.gmi

Gemini request details:

Original URL
gemini://yujiri.xyz/software/javascript.gmi
Status code
Success
Meta
text/gemini; lang=en
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.

What is Gemini?