Michal Paszkiewicz

js quirks that always get me

Anyone who has done half a course on programming knows that javascript has its quirks. Some get very upset over them, while others lovingly collect them and spend their spare time stroking them, whispering "my precious". There are some people who claim they fit in neither of these groups, but they are just living a lie.

Before you start wondering which of the groups I fit into, I would like to make it clear that I am not going to use this article to rant about the javascript. Ranting about the javascript would be like ranting about the Batman. It is not the scripting language we need. It is the scripting language we deserve. I am writing this article because I make mistakes and there is no better way of learning from your mistakes than by announcing to everyone you made these mistakes.

People will laugh at you. They will bring it up in conversations about gardening. They will draw pictures of you with silly speech bubbles. Once you have suffered this, you will never make that mistake again. I hope.

Anyway, I am doing quite well at remembering most of the normal js oddities that people bring up when they are really failing to impress their dates. However, there are always mistakes that surprise me, even if I have done them a few times before. Here they are!

Replacing dollar signs

Now most of you will immediately laugh at me because you've all read this. Or you were already aware of dollar signs being involved in "special replacement patterns". Either way, when you are working with MathJax and need to insert some beautiful Latex in an html page and for some reason you decided to do this with a string replace, please remember me and put a slight smirk on your face.

"blah".replace(/blah/g, "$$");
// output:
// "$"

The worst part is I keep forgetting about this issue. I blame it on the fact that I'm only used to seeing £s disappear.

0 indexed months

In the USA, they write 8/16/2016. In England, we call that an abomination and write 16/8/2016. Considering we've had clocks for longer, it makes sense that we have the better convention for writing dates. In javascript, you would write 16/7/2016. I'm not going to argue that this is silly or wrong, because we all know the British have had clocks for longer than javascript has, so there is nothing to prove.

Either way, here is a beautiful example of the way javascript works:

new Date(2000,1,1);
// output:
// Tue Feb 01 2000 00:00:00 GMT+0000 (GMT Standard Time)

No matter how many times I write some code with dates, I always forget about this and turn up a month late to meetings. In all honesty, the best reason to learn javascript is that it provides plenty of excuses as to why something has gone wrong.

published: Wed Aug 31 2016

Michal Paszkiewicz's face
Michal reads books, solves equations and plays instruments whenever he isn't developing software for Lowrance, B&G, Simrad and C-MAP. His previous work at TfL has left a lingering love for transport.