102 comments

[ 4.6 ms ] story [ 169 ms ] thread
This is incredibly funny, but does anyone know why the interpreter makes such bizarre decisions? Why wouldn't it be preferable to just throw up errors?
Welcome to the world of JavaScript. This response is NaN.
Javascript was a rush job. And the ECMA spec was needed early due to industry pressure. So where perl and (to a lesser extent) python were able to just bury their early goofs in a pile of incompatible versions bumps, Javascript had its encased in the proverbial carbonite of an international standard. Which is sad, really, because in a lot of other ways it's the cleanest scripting language of its generation.
For more sadness, see: http://wtfjs.com

Fun fact: the creator of Javascript posts on HN. Maybe he'll answer your question.

The Ruby stuff apparently isn't bizarre. From what I've read, Ruby creates variables as it encounters an assignment to them when parsing the code. So, when you have

   a = a
in your code, Ruby creates the variable a before it ever tries to actually execute the assignment. If later, when it has finished parsing everything, and starts executing, that statement fails because b is not defined, you still end up with variable a being defined, and since it has not had anything successfully assigned to it, it has a value of nil.

The JavaScript stuff, I think, comes from operator overloading. The plus operator is overloaded to allow adding strings to concatenate them, and it will do type conversion to get compatible types, so "wat"+1 results in the 1 being converted to a string, and then the strings are concatenated. Since the minus operator is not so overloaded, "wat"-1 instead is treated as numerical subtraction. JavaScript allows string to be used as numbers, so "123"-1 gives 122. However "wat" is not a string that represents a number, so gives NaN when forced to be treated as a number, and "wat"-1 is thus NaN.

(comment deleted)
Thanks for that. I was curious the reason, it seems obvious though after an explanation.
(comment deleted)
JavaScript has a strong design principle of not throwing errors for syntactically valid constructs. The most common way it accomplishes this is through gratuitous use of implicit type conversions. It does a lot of implicit string conversions, even on things that you would think of as error codes, like undefined or NaN.

JavaScript went down the path of being a "forgiving" language because it was intended to provide "extra" (non-core) functionality, under a lot of environments, in the hands of non-experts. It made sense at the time that it should fail silently rather than noisily to not crash the page it was on. It also tried to help amateurs who just banged on code until it did what they wanted, by generally doing something rather than nothing, and by trying to infer intended behavior from undisciplined code. It was never intended to make large-scale or robust applications, so it didn't make decisions that would facilitate that use-case.

I wish there was some technology that could take a bunch of code and warn you of common errors before having it execute and 'noisily crash the page it was on'.

Maybe someday...

Sarcasm doesn't work here. It's not reasonable to expect proper testing or usage of an IDE by ordinary citizens authoring HTML. Specially in 1996.
Yeah, I mean, it's not like they had compilers back then. Savages.
Hilarious! This reminds me of a StackOverflow question which asked the users for the weirdest language quirks they could think of. I laughed pretty hard at that too.
This is a repost, but whatever. Hopefully people who haven't seen it before will enjoy it.
Even with my limited programming knowledge I laughed a lot through this.
At work people always tell me "You IT guys are all so funny."
I laughed so loud I scared myself. Now, I know why my wife looks at me funny.
Wow, I laughed so hard I cried! This is hilarious...
wow, you must be the best hacker ever congrats
I understand the downvotes since this is HN and you people don't like useless comments here. But I simply thought it was very funny. No need to be mean here.
For the record, the downvotes aren't to be mean. We just don't want this turning into a Reddit clone and/or fill the comments with run-of-the-mill comments you'd see on any PHPbb forum.

No, I wasn't one of the ones who downvoted you and no, I'm not trying to be mean. There used to be times I wondered why an innocent comment of mine was downvoted and thought I'd just let you know instead of leave you hanging.

Thanks for the reply. Perhaps I was unclear. I completely understand the downvotes - that's why they're on the site. But I thought the "wow, you must be the best hacker ever congrats" comment was unnecessary and mean-spirited.
Does anybody knows which is the javascript interpreter he is using on the screencast?
It's jsc (comes with Webkit) - it's present on Mac OS X by default in

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc

You can just do:

sudo ln /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /bin/jsc

To be able to invoke it directly from the command line.

Thanks, I'll look how to get this working on linux.
At least Arch has `js`package which gives you js command to obtain JS shell; in fact it is just Mozilla's SpiderMonkey. Anyway, the best option for "standalone JS" is node, mainly because it has a sane way of importing code from other files.
My Chrome install on Windows has the same thing. I push ctrl+shift+c to load the developer tools, and click on console and it's a very similar tool to what he was using.
That is extremely useful. Do you know if any testing libraries or other useful functionality have been built around this?

I suppose if you are a WebKit-only developer, this utility could have many useful applications.

There's also mozrepl, which lets you evaluate code in Firefox from the command-line or Emacs.
<shamelessplug>If you want to use any of these inside emacs check out my js-comint mode: http://js-comint-el.sourceforge.net/ Makes it really easy to do things like write code in one buffer and execute it in another. </shamelessplug>
Quicktime? We don't need no stinking web standards!

   $ php -r '::'
   Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
Wat
Pffth, you just need to learn Hebrew before you learn PHP. That says "double dot twice".

  $ php -r '::'
  Parse error: parse error in Command line code on line 1
How'd you get the weird error?

Edit:

Hrm, if I try it on my DreamHost account, I get something more similar to yours:

  $ php -r '::'
  Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in Command line code on line 1
This was introduced by Zend, which is israeli. Thus the Hebrew.
I first saw that error in a pre-Google world. It took a few days for me to figure out what it meant.
This is the greatest video ever.
What presentation software is he using? Is he just jumping from his slide deck into a shell?
It's Keynote. I recorded those sessions as screencasts, then sliced them into tiny pieces so I can advance them perfectly with my talking. There are 37 slides in that four-minute talk. In some cases, the slices are only two or three frames long.
Ah that makes sense, thank you. I may steal that idea for future talks with R. Though, just embedding a shell in a slide deck would be very nice.
I like the sliced screencasts because they're so repeatable. I practice my talks a lot, and I can get the timing down perfectly because it's always the same. Highly recommended. :)
The preparation really shows -- great job. Ability to present really well is a critical skill for anyone with a product head.
Guess I'm not a hacker? It's like watching a rerun I've seen twice over with the laugh track turned up to 11 (just to clarify, someone said it was a repost but I haven't seen it before).
There's no laugh track; that's the audience.
(comment deleted)
Yeah count me in the "not a hacker, I guess" crowd, this stuff isn't very funny.
"Wat" at the start really turned me off. I realize that was probably (?) part of the shtick, but it's one misspelling in particular that really drives me nuts. Real hacker humor is fairly dry (the story about magic comes to mind); this felt more like script kiddie-level humor. I can see valid points (and humor) raised with the Ruby interpreter, but the delivery could have been better--at least more adult-ish.

I managed about halfway through. I did have the audio turned off, so I'm assuming I missed out on some of the redeeming qualities it may have had. Given the liberal usage of "wat" throughout what I did watch, I suspect that my gut feel is likely correct.

Right. It was marginally informative, the meme pics were chuckle-in-my-head worthy but what really got me laughing was that it's on the front page of HN. Its so sad you have to laugh to keep from crying.

Anyway, guess I'm not a hacker either.

Not even close to as sad as the fact that two most upvoted comments and their children (pun intended) are just a bunch of pompous whining.
"Pompous?" Perhaps, but I think you'll agree that there are far more redeeming and clever ways to construct a humorous commentary on the behavior of (for example) an interpreter.

To address your particular concern which I feel is somewhat misplaced: I think it would be more pertinent to classify individuals like myself and the others who have complained about the delivery of this particular piece as suffering from less patience, especially with the usage of "wat" throughout. There are reasons for this, and it likely depends on an individual's preferences, how one feels about the (mis)usage of language, or the (ab)use of memes to attempt the creation of a witty informational exchange. Note: I realize that I'm probably just slightly outside the target demographic of this video, which probably appeals more to individuals who are in their mid-twenties or younger, and I'm fully aware of this fact.

I apologize if I personally came off as a pompous whiner. This particular misspelling of "what" is a personal pet peeve that drives me absolutely insane, and it greatly reduces my mental perception of an item's quality (or the quality of an individual's speech). I suppose other complaints probably derive from its delivery as a meme-inspired video, which probably appeals to younger audiences as I've mentioned.

In other words: There are legitimate gripes about this form of video. I've seen similar ones that were indeed humorous, but they had a fantastic delivery that was well thought-out. Including a dozen absurd pictures that appear to be sourced from sites like Imgur with "WAT" emblazoned on them does not appeal to everyone, and I don't think it's useful to criticize others' opinions of this video as sorely lacking in humor as "sad" simply because we all have greatly different tastes and preferences. Moreover, these tastes and preferences are malleable; something you found hilarious when you were 22 might be head-shakingly awful by the time you're in your 30s.

I suspect this video is one of these. Save it and watch it again in ten or fifteen years.

Also, if you want a good example to serve as a useful benchmark for classical "hacker humor," I present to you this piece: http://catb.org/jargon/html/magic-story.html

I suppose I came off as snarky with my comment but I still don't think this was HM worthy. My reason for disliking it was because it simply showed a bunch of examples of obscure flaws in languages that aren't really useful. There wasn't enough substance to it. It was just "look at the weird result you get when you write code in a way that you never would in the real world. Wat?". Meme pics are funny. Meme pics and a good talk are funny. This was meme pics and a really useless, trying too hard talk. I've seen presentations that offer up a lot of value in terms of stuff you can actually use and topics that are really relevant. This was just language quirks and nothing more. No talk about why these quirks exist, uses for them, how they could affect you without knowing, none of that. It was just bad humor and no substance. I'm not against humor showing up on HN from time to time but this was more like something that belongs on Reddit or even any random forum. Oh, and the title too bothered me. It was kind of immature.

And for the record, I'm 25 so either I'm really mature or I have no sense of humor. The jury is out. I'm definitely still not hacker though as I watched it again and still barely chuckled.

Eh, I'm sure my original comment was somewhat snarky, too. I simply cannot shake the feeling that you were downvoted because there's a large number of people who find the meme overuse outright hilarious (I don't; if I want memes, I'll visit Imgur's gallery--they can be funny, but I found their use in the video, well, useless). It's a shame, too, because I felt that there was a fairly strong sentiment in agreement with you (including myself, so I'll admit bias).

Regardless of what others might feel, you're absolutely right. The title had about as much redeeming value as the video. :)

I'll join you in the non-hacker, no-humor camp. This is particularly true since my inner grammar Nazi finds the abuse of "what" to be abhorrent...

Really? You don't think this is funny at all? Maybe it's because I'm in my mid twenties, but I think my father would even find this funny and he knows little to none about programming.
My (non programmer) wife found this hysterical. It reinforced her belief that programming is stupid. :)
If there were jokes formed around the nonsensical behavior of these languages, then it might be funny. But I don't find it very entertaining to just point them out.
The humor is playing on an inclination to laugh at things we don't understand, which seems counter to the hacker ethos. Software is hard, and there may be deep reasons in the dynamic type systems that cause these languages to work like this, or there may not be, but either way this type of thing is interesting and seems to demand some further explanation and/or justification. I find it interesting but don't get the humor. Perhaps, as you implied, I'm just too old.
> The humor is playing on an inclination to laugh at things we don't understand

I think is simply laughing at absurdity: the gulf between intention and actuality. We have quite a bit of that.

I didn't laugh because I was so worried that if I didn't laugh I wouldn't be considered a hacker :(
I didn't laugh until the end. Partially, I'm sure, because I didn't want to lie to myself by intentionally laughing just to feel like a hacker. But when I saw NaNNaNNaNNaN...(etc) coming, I couldn't help it.
Your comment made me laugh a lot! Thanks.
Interestingly the results from jsc, are different from node.js:

----- jsc ------

   [] + []
   
   > [] + {}
   [object Object]
   > {} + []
   0
   > {} + {}
   NaN
------ node.js ------

   > [] + []
   ''
   > [] + {}
   '[object Object]'
   > {} + []
   '[object Object]'
   > {} + {}
   '[object Object][object Object]'
I don't even understand what the last node.js entry is. Is that an array with two objects?

Also, I'm on an old version of node, but my output matches jsc.

The `toString` of two Object-s concatenated, I think.
The other reply is right, to clarify String({}) === "[object Object]"

I think the actual video was a little misleading, [] + {} == the string "[object Object]" not an object. The square brackets are just part of the tostring method and are unrelated to the square brackets of arrays.

This appears to be a peculiarity of the REPL implementations, not a difference between V8 and JSC. d8 (and the Chrome console) behaves the same way as jsc, while you can make d8 and jsc behave the same as the node REPL by wrapping each statement in parentheses:

  d8> ([] + [])
  
  d8> ([] + {})
  [object Object]
  d8> ({} + [])
  [object Object]
  d8> ({} + {})
  [object Object][object Object]
So I believe this is merely erroneous behavior of the console, not a weirdness of JavaScript itself.
No, it's more subtle than that. By wrapping in parenthesis, you're making it an expression. When you just say "{}+[]" in e.g. Chrome, the first {} is parsed as a block. So what you see printed is the result of "+[]", which is 0. This is why {}+[] is not equal to {}+[] without parens. This may also be why Node gives different results; I'm not sure.

I didn't mention any of this in the talk (it would've killed the flow ;). Instead, I glossed over it and interpreted the syntax as any sane programmer would.

I understand why it happens. I'm saying that what you're pointing out is not in the nature of JavaScript, it's in the REPL. It's just wrong; there is no other situation where that code would evaluate that way. node is behaving "properly", showing you what value you can expect if you, say, assign that expression to a variable.

I guess it's an amusing bit of sleight-of-hand, but using it to mock JavaScript seems, I don't know, tasteless. Doesn't it have enough problems without inventing more?

It's a joke. Relax.
Yes, I saw the joke.

To clarify, I thought the first part of the talk was funny. Here's a weird bit of Ruby. Hah! Here's a weird bit of JavaScript. Hah! Here's some JavaScript to make you think it's weird in a way it actually isn't! Uh, okay? Just don't see what's funny about that, I guess.

Wow, this title suffers from some serious editorializing. What's next, "10 ways to know if you are a hacker? (cracked.com)"
I feel like we've been suffering extra-much from this effect lately...
(comment deleted)
How does this have anything to do with a sense of humor specific to hackers? Don't all professionals enjoy jokes about their field?
That is exactly what I was wondering: what you say must be true, but I could not find any equivalent of what you see here for any other profession.

But maybe it is just because I write code for a living...

What's the "correct" ("less funny"?) result of

   Array(16).join("wat" - 1) + " Batman!"
?

Empty strings separated with NaNs look perfectly logical to me (I'm a JS guy though, so, well, bear with me).

That's probably one of the more logical examples. I'd have expected an exception from any other language, though (Javascript I know doesn't tend to type error).
Well, .join(x) joins an array into a string, with x as the separator. First of all what does Array(16) make? Effectively it should make an array with null 16 times. Now, JavaScript seems to think null is a blank string, a string with the text 'null', or a zero depending on the context.

The first thing it should do is TypeError that null is not a string. Or don't join null fields because there is nothing there.

If is does convert null to '', then it should TypeError on string - int. If it is not going to TypeError it should do something sensible, like slice the string

  > Array(16).join("wat".slice(0,-1)) + " Batman!"
  'wawawawawawawawawawawawawawawa Batman!'
I think the correct thing would be to TypeError in two different places. Or change the language so things that don't error actually work. Returning a string with NaNs in it will just lead to uncaught errors. If it actually returns something it should probably be ' Batman!'.

JavaScript nuttiness:

Now it's zero:

  > null + 1
  1
Now it's "null":

  > null + "text"
  'nulltext'
Now it's a blank string:

  > [null, null, null].join()
  ',,'
Python errors when things don't make sense, and forces you to make them make sense:

If it doesn't work, error:

  >>> "wat" - 1
  Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
  TypeError: unsupported operand type(s) for -: 'str' and 'int'
Can't join None into a string:

  >>> ','.join([None, None, None])
  Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
  TypeError: sequence item 0: expected string, NoneType found
Convert None to a string if you want:

  >>> ','.join([str(i) for i in [None, None, None]])
  'None,None,None'
But it makes more sense to drop the None:

  >>> ','.join([str(i) for i in [None, None, None] if i])
  ''
Array(16)

That's 15 commas, not 16.

Handy way to make some padding:

    function pad(n, c) { return Array(n+1).join(c); }

    // pad(6, "-") === "------"
if you laugh, you know ruby.
why do i have to signup to know who much it costs to subscribe. I think it should be apparent on the home page.

EDIT ok my bad it was mentioned on the screencasts homepage

It's on the front page, but at the bottom. DAS is a small one-man business, so I did the design myself (and I'm not a good designer). Sorry about that. :)
I guess you had to be there. I can see why someone might find this funny, but honestly, nothing is that funny. Some behavior in Javascript is surprising, but not that surprising.
If you've ever gone through a phase (or a graduate degree!) of thinking deeply about programming language design, you might, as I do, find Javascript's expression evaluation behavior to be just about the funniest thing around.
I have but I don't. Javascript was designed to reduce programmer-visible errors at the cost of making it nearly impossible to write a correct program. At that goal, it succeeds, and [] + {} being NaN makes perfect sense under those constraints.