Revisiting Javascript && some lols


I’ve been working with Javascript off and on for years now. During my internship I wanted to update my knowledge since I was not familiar with ES6 and Node.js. My mentor taught themselves Javascript and Node so they recommended a few books and exercises I could do. I took them up on that offer and am reading Eloquent Javascript.

Chapter one reminded me of this really funny article I read in Usenix by Professor James Mickens. Here are some of his gems:

◆ JavaScript is dynamically typed, and its aggressive type coercion rules were apparently designed by Monty Python. For example, 12 == “12” because the string is coerced into a number. This is a bit silly, but it kind of makes sense. Now consider the fact that null == undefined. That is completely janky; a reference that points to null is not undefined—IT IS DEFINED AS POINTING TO THE NULL VALUE. And now that you’re warmed up, look at this: “\r\n\t” == false. Here’s why: the browser detects that the two operands have different types, so it converts false to 0 and retries the comparison. The operands still have different types (string and number), so the browser coerces “\r\n\t” into the number 0, because somehow, a non-zero number of characters is equal to 0. Voila—0 equals 0! AWESOME. That explanation was like the plot to Inception, but the implanted idea was “the correctness of your program has been coerced to false.”


◆ Hello, kind stranger—let me keep you warm during this cold winter night! Did you know that JavaScript defines a special NaN (“not a number”) value? This value is what you get when you do foolish things like parseInt(“BatmanIsNotAnInteger”). In other words, NaN is a value that is not indicative of a number. However, typeof(NaN) returns… “number.” A more obvious return value would be “HAIL BEELZEBUB, LORD OF DARKNESS,” but I digress.


◆ By the way, NaN != NaN, so Aristotle was wrong about that whole “Law of Identity” thing.


◆ Also, JavaScript defines two identity operators (=== and !== operators) which don’t perform the type coercion that the standard equality operators do; however, NaN !== NaN. So, basically, don’t use numbers in JavaScript, and if you absolutely have to use numbers, implement a software-level ALU. It’s slow, but it’s the only way to be sure

To Wash It All Away by James Mickens Pg 7

LOL … After doing some C, and looking back on Javascript’s quirkiness I definitely sympathize with his point of view.