JavaScript stupidities

Kragen Sitaker kragen at pobox.com
Mon May 30 03:37:01 EDT 2005


JavaScript is a nice language, but it has a lot of rotten little
problems.  Here's my catalog.

[,,,] is valid syntax and is equivalent to [undefined, undefined,
undefined].  Note: the length of this list is 3, not 4.

"" + undefined is "undefined", not an error or the empty string.

[1,2,3].toString() is "1,2,3".  Which means that [1].toString() is the
same as (1).toString(), and [[[[]]]].toString() is ""!

[1, 4] + [2] results in the string "1,42".

This is a simple example of the general problem that JavaScript has no
operator overloading.

Low-level languages often have looping constructs limited to a few
minimal kinds of loops.  C generalized these, but most high-level
languages (those with the concept of a list) have a different kind of
looping construct: one that binds a loop variable in turn to each
element of a list.  Python spells this "for variable in [a, b, c]";
Perl spells it "foreach $x ($a, $b, $c)"; Smalltalk spells it
"{a. b. c} do:"; Lisp calls it "mapcar".  You'd think the JavaScript
construct "for (x in [a, b, c])" would do the same thing.  Instead, it
loops x over the *indices* of the list --- that is, 0, 1, and 2.
There's no convenient way to loop over the items of a list, but if you
store the list in a variable, you can fake it, like so: 
var mylist = [a, b, c]; for (var x in mylist) do_something(mylist[x]);

Worse, that only works on real Arrays.  Other things, such as
window.frames, that merely support an array-like interface, require
the more verbose "for (var ii = 0; ii < something.length; ii++)".

There is no module system.

There is no standard for documentation analogous to perldoc, Python or
elisp docstrings, or Javadoc.


More information about the Kragen-tol mailing list