Best comparison: Python review
Dynamic typing up to 11
Subtle benefits of static typing
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?
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).
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
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
And this one is even funnier:
Because objects are always true.
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.
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.
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.
subscribe via RSS