Javascript is a Botch, Part 1 (Information Technology)

Paul M. Foster (06 October 2011 15:46:46 Revised )

This will be the first in a series of rants about Javascript. I haven't used Javascript much; only as much as is necessary to set up some AJAX activity in some of my corporate software. I've been studying it off and on via Javascript: The Definitive Reference from O'Reilly. But from what I can see, this language looks like the ultimate botch. So with that as my premise, let's dig into some reasons why I think that.

  1. Anonymous functions as parameters for other functions.
  2. Seriously? Okay, here's an example of how this works:

    function alfa(function () {
    	bravo = 2;
    	charlie = 3;
    }) {
    delta = 4;
    echo = 5;
    }
    

    Did I just stumble into the modern day incarnation of Lisp or something? And this type of construct in Javascript is common; I see it all the time. Trying to figure out what this does seems like a nightmare to me. It is routinely done this way for much larger and more complex functions, making the task that much harder.

  3. All numbers in Javascript are represented internally as floating point.
  4. You heard right: floating point. Even integers are represented as floats internally. Do I really need to explain what a bad idea that is? I suppose numeric precision wasn't important to Javascript's designers.

  5. x++ is not the same as x += 1
  6. var i = "1";
    i++; // i becomes 2
    

    var j = "1"; j += 1; // j becomes "11"

    Any scripting language which is vaguely typed like Javascript and PHP will make some type conversions in the background. This one has peculiar side effects, partially because of the choice of "+" as the concatenation operator. (I wish PHP had selected "+" as the concatenation operator instead of ".". Still a botch, though.)

  7. Javascript inserts semicolons when it thinks you omitted them
  8. Consider:

    return
    true;
    

    C, PHP and any number of other languages will assume you meant:

    return true;
    

    Javascript assumes what you really meant to say was:

    return;
    true;
    

    Um... can we avoid having the language assume it knows what I meant to do, and just follow my instructions (even if wrong)? That way I can at least, maybe track down the error. Instead, the language creates an error for me to find, and in order to find it, I have to remember that Javascript makes some assumptions about semicolons and commands on their own lines.

  9. Three ways to define functions.
  10. To wit:

    function foxtrot(golf) { do_stuff(); )
    var other_stuff = function hotel(india) { do_other_stuff; };
    var more_stuff = new Function("x", "return x + x;");
    

    The middle version has peculiar properties when you drag the "other_stuff" variable around. And don't even get me started on the last variant. Who comes up with this stuff?

    The treatment of functions and their results is by far the weirdest thing I have so far found in Javascript.

Stay tuned for more later....