Why JavaScript is not my favourite programming language

Ahem.

Falsy Values

JavaScript has a surprisingly large set of falsy values, shown in Table A-1.

Value                Type
0                    Number
NaN (not a number)   Number
'' (empty string)    String
false                Boolean
null                 Object
undefined            Undefined

These values are all falsy [i.e. they test as false in conditionals], but they are not interchangeable.  For example, this is the wrong way to check if an object is missing a member:

   value = myObject[name];
   if (value == null) {
      alert(name + ' not found.");
   }

undefined is the value of missing members, but the snippet is testing for null.  It is using the == operator (See Appendix B), which does type coercion, instead of the more reliable === operator.  Sometimes these two errors cancel each other out.  Sometimes they don’t.

undefined and NaN are not constants.  They are global variables, and you can change their values.  That should not be possible, and yet it is.  Don’t do it.

– Douglas Crockford, JavaScript: The Good Parts, Appendix A: Awful Parts.

Case closed.

About these ads

8 responses to “Why JavaScript is not my favourite programming language

  1. So now ask me why the department I work in think that Javascript is an acceptable first language to teach students ‘because they can see results in the browser’

    Actually, no. don’t. I don’t have a f-ing clue either.

  2. As the “assembler” of the web, javascript can be targeted by a variety of other languages that may be more to your taste, my favorite is haxe (www.haxe.org), but there’s also, coffeescript, google closure, parenscript, ocamljs, websharper (f#) etc. If one is going to use jslint, then why not use a real compiler and forget these annoying details.

  3. I’ve only recently started to dip a toe into Javascript, but the way different frameworks and libraries roll their own inheritance models (out of necessity, I admit) bugs me a bit.

    Not that you can’t do similar things in other languages, it’s just that Javascript almost seems to require such efforts for any sizable Javascript project. However, I do admit rolling your own inheritance models provide quite a bit of flexibility.

  4. I started JavaScript with “The Good Bits”, and therefore I like it pretty much (even though it doesn’t come close to Python).

    Especially the prototype model is truly beautiful once you know it’s there. It’s just that, even if you yourself are using only the good bits, you always come across those non-good bits whenever you read someone else’s code:-(

  5. Agreed. ecma-script is such a mess.

    And a single tested function is doomed to be fully working today and not working tomorrow due to some “improvements” on the container (yeah I’m talkin’ about browsers) or some magical update of the OS, or simply ’cause the end users has changed the hosting browser, that sucks. Why in 2010 I’ve to write custom pieces of code for browsers?
    That alone makes me consider javascript as true crap.

  6. I love this series! Every language has problems. I can’t wait until you get to C# (my current language of choice).

  7. The silent, error-swallowing menace that is “undefined” deserves to be killed with fire and its grave salted.

  8. JS itself, separated from browser shenannigans (as in Node.js or Narwhal, for example) is actually quite good language. IMO several falsy values isn’t that much problem. Reloading of “undefined” is a problem, though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s