Go home, JavaScript, you are drunk

Using rhino, the command-line JavaScript interpreter:

mike@brach:~$ rhino
Rhino 1.7 release 3 2012 02 16
js> []+[]

js> []+{}
[object Object]
js> {}+[]
js> {}+{}

From Gary Bernhardt’s classic lightning-talk, Wat, which is very well worth five minutes of any programmer’s time.


9 responses to “Go home, JavaScript, you are drunk

  1. I’ve been meaning to learn some javascript. So Rhino seems cool.

    Yeah that up there, that is some weird behavior, I agree. I (perhaps naively) wonder if it might have something to do with how Javascript is one of those that uses defaults heavily to avoid ever having to deal with undefined values.

    Furry cows moo and decompress.

  2. No, I don’t think even JavaScripts unpromising history can explain this level of randomness. There are qualities of JS that derive from that history, as Douglas Crockford’s useful (and short!) piece JavaScript:
    The World’s Most Misunderstood Programming Language
    shows. But this seems to be sheer brain-damage.

    Other examples:

    • typeof NaN === ‘number’ // true
    • NaN === NaN // false
    • NaN !== NaN // true
  3. That said, there’s no denying that JavaScript is fantastically useful, not to mention ubiquitous. The best place to start is probably Doug Crockford’s little book JavaScript: The Good Parts [amazon.com, amazon.co.uk], which is only 154 pages long including the appendices, yet tells you all you need to know and — crucially — omits much that you are better off not knowing.

  4. »Wat« was quite fun … to drive the pain home, I recommend going through these: http://javascript-puzzlers.herokuapp.com

  5. Also interesting:

    js>[] + {} === {} + []
    true (!)

    I was willing to blame the REPL, since I assumed it must be implicitly converting everything to strings, but this is easily disproved:

    js>[] + {} + “”
    “[object Object]”
    js>{} + [] + “”
    js>[] + {} + “” === {} + [] + “”
    true (still!)

    So… yeah. Kind of a weird language.

  6. [] – [] = 0


  7. In comparison:
    js> {} – {}

  8. David Starner

    NaN != NaN. That’s not JavaScript, that’s IEEE 754 floating point numbers, and pretty much any language that features a NaN will respect that property.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.