Charlie Harvey

Javascript: the weird parts

Javascript. Love it or hate it it seems to have become the defacto virtual machine of the internet that Java was supposed to be. And it is odd. Like properly odd. This is a short collection of some weird things that I have noticed about it.

JavaScript: the weird parts

I keep a little list of these by my desk for my own sick amusement (which is something that my pal oxguin reckons is not a fact to be shared out loud). If you like this little lot, then you would probably enjoy some of the prior art at wtfjs.

Comparison craziness

Let’s start by making x an array with 0 in it.js> x=[0] [0]It should equal itself, which is good.js> x==x trueBut it also equals not itself, which is not so good.js> x==!x trueWhat about comparing a 3 element array and a string? js> Array(3)==",," trueRi-iight.

Type twattery

The type of not a number is, well, a number. Sensible.js> typeof NaN "number"Well at least that means it ought to be equal to itself, right?js> NaN==NaN falseSo much for NaN. I wonder what the type of null is …js> typeof null "object"… but of course it is not actually an instanceof Object js>null instanceof Object falseSo, what is a string then?js> "string" instanceof String falseUh-huh.

Bounds buffoonery

How about a really big number first of all. Wait. What?js> 9999999999999999 10000000000000000Hopefully small numbers make more sense.js> 0.1+0.2==0.3 falseI guess not. I bet that max is still bigger than min though.js> Math.max()>Math.min() false js> Math.max()<Math.min() true Maybe not.

Array assinity

Calling + on an empty array and an empty array gives the empty string. Who knew?js> []+[] ""How about an empty array and an empty object? js> []+{} "[object Object]"An empty object and an empty array gives, ummm, 0.js> {}+[] 0Whereas an empty object and an empty object is not a number. Which is true, I suppose.js> {}+{} NaN

Boolean balderdash

I’ve heard of that boolean arithmetic. Let’s give it a try.js> true+true===2 true js> true-true===0 trueAh. It looks like true is equal to one. I’ll just check.js>true===1 falseDoh!

And one for luck

See if you can work out what this will evaluate tojs> (!+[]+[]+![]).length

Wow! Thanks for reading this far. Got any favourite weird parts of Javascript? Share them in the comments.

Incidentally, these were all evaluated on spidermonkey 24.2.0. Maybe they won’t work in node/whatever other js engine you are using…


  • Be respectful. You may want to read the comment guidelines before posting.
  • You can use Markdown syntax to format your comments. You can only use level 5 and 6 headings.
  • You can add class="your language" to code blocks to help highlight.js highlight them correctly.

Privacy note: This form will forward your IP address, user agent and referrer to the Akismet, StopForumSpam and Botscout spam filtering services. I don’t log these details. Those services will. I do log everything you type into the form. Full privacy statement.