JavaScript Chose Violence
I've been writing JS since web apps were called "DHTML".
And maybe I'm crazy, but I like JS.
These days I write TS, but mainly as a hair loss treatment.
But let's be honest... for a language initially created in 10 days, it doesn't disappoint.
There are some corners of the language that make 10 days sound like a long time.
JavaScript is now the world's most used programming language.
Consequently, "wtf" is now the most common word in spoken English.
Well, mostly... it sometimes leaves the body of JS programmers as a demonic scream.
And for good reason.
So, in the spirit of appreciating history, and cementing our disbelief about our entire industry, let's take a short tour through the slums of JS.
1: George Washington was born in the year -168
That's right – the Date type in JS subtracts 1900
for you, because let's face it, nothing existed before 1900 anyway.
The creator of JS actually stole this from Java, which raises serious doubts about the validity of the saying
"JavaScript is to Java as Pain is to Painting"
Now if you thought that was weird...
2: He was also born in January...
Why should months get away with not following our zero-based numbering system?
Well, not on JS's watch!
None of this matters anyway, right? We can just add 1900
, or 1
, back to these results to correct them.c
Because...
3: Sure, JS has numbers, and they mostly work...
Did a demonic scream just leave your body? Don't worry you're not alone. Totally normal.
Listen, don't panic! Just remember that 0.1
+ 0.2
in JS is actually 0.30000000000000004
.
I mean, that's pretty close!
Hopefully it'll come as good news that JS doesn't get ALL fundamental things wrong.
Oh, wait, hang on...
4: Equality in JS
We all know this one! Easy fix – always use triple equals!
Don't worry, this is an edge case as well.
Strict equality in JS is just as good as triple-cooked fries in pretty much every other situation.
For example:
5: NaN
actually is NaN
Phew, finally!
Oh, no, wait...
There's that demonic scream again!
PS: Number.isNaN()
has your back.
6: Parsing numbers
Most of us know that parseInt()
should be given a second argument to also specify the number base, narrowing missing the presumably vast use-case of people wanting to parse octal numbers...
The good news is that in almost all JS environments these days, that is fixed. No more weird behaviour from parseInt()
.
He's done his time and he's turned over a new leaf...
Sore throat? Don't worry, it hardens over time.
Let's discuss some obviously good bits of JS advice.
7: Never use eval
And never accidentally use something that uses eval
...
To be fair, if you're writing code that includes its own code in strings, you should probably just switch to C++ and proverbially blow your whole leg off much more elegantly than this.
At least have some class!
Speaking of which...
8: JavaScript has classes... well, kind of.
Classes were added to JS so we can pretend to do OO programming without really doing it.
It was a political policy to secure future refactoring and rework jobs for years to come.
9: And it's not the only thing that seems familiar but ain't
A return value from finally
always overrides whatever happens in try
.
Probably weird if you're coming from most other try/catch
languages.
Still, variety is the spice of life!
10: Arguments shmarguments
Of course, Math.min()
and Math.max()
here are supposed to have things given to them.
And when they do, they behave as one might expect, which at this point is rather surprising on its own.
But without arguments they are yes-men, running off into execution to do something that you probably don't want them to do.
11: This ain't Ruby!
Thank god!
Finally some sense from this language!
42
is a primitive number, not some kind of object. We can't be calling functions on it...
How do I uninstall JS?
No, seriously.
Does anyone know how the market for tending to farm animals is right now? Any roles going?
Before we bow out, go for a long lie down, and try to forget this post ever existed, I'd like to share a secret with you.
12: Arrays in JS have secret rooms where you can hide stuff...
How wonderful.
I hope you enjoyed our brief (and sadly, it could have been longer) tour of the slums of JS.
---
I hope folks take this post with the jest in which it was written.
The truth is, JS does absolutely have some ugly parts.
It's useful to know about them, and know how to avoid them.
At the same time, JS has really improved since it was 10 days old.
These days, most of us write TypeScript, or use other tools in vanilla JS, to automatically prevent all of these issues.
And in the end, JS is here to stay.
With a little wriggling, we can get out of most of its traps and enjoy some of its real benefits.
It's not all bad.
As Bjarne Stroustrup said:
"There are only two kinds of languages: the ones people complain about and the ones nobody uses."