Stupid things about Javascript

There are many reasons one can hate a programming language. But for Javascript one can never find enough reasons. They keep coming out of nowhere. Javascript blew up too fast which meant that there never was enough time to correct its mistakes as they become part of the standard.
Now let’s discuss a few weird stuff it includes.

So in the above case, we substract 3 from a string that contains the character 5. Weak types and implicit conversions in Javascript brings us the output of 2.

But what happens here? Why are we getting 53? Well in this case, 3 was implicitly cast to a string and we have string concatenation instead of integer math.
So the first case converted ‘5’ to 5 and the second case 3 to ‘3’.

What about this?

Again, both strings were converted to integers. Why? Who knows? Javascript is amazing.

Ok now let’s try something else.

Ok the + + did not change anything in the end, we still get string concatenation. So what would happen if we tried with two strings?

Wait what? So in the first case it works but in the second it does not? Yes, because in the first case, the number 4 was converted to an integer and the back to string in order to be concatenated with ‘5’. Why? Because of this

So when we were doing the same code with two strings, Javascript attempted to convert the string containing bar into an integer, which resulted in NaN and then converted NaN to a string. Marvelous, don’t you think?

Next one

Yup. Makes sense does it not?
-‘4’ gets converted to -4 and then back to ‘-4’.

What is happening here? Well here we see a simple butchering of math.

Let’s move one to other stuff.

Ok, so the type of Not a Number is a number? And it also does not equal itself? Oh, well. Let’s try equality with other stuff.

Ok so far. Makes sense that an array with only one value in will equal itself. Continuing on the above example let’s try this

Wait so x equals itself, but also equals not itself?

Ok. What about the following?

Great, nice, awesome….

So what is null really?

Ok, so which means

Wait, typeof null is object, but null is not an instance of Object? Ok. But surely this would make more sense with a string.

Ok, so which means

No? ok then. Let’s try some max and min values.

Really? So the max value of math is smaller than the min value of math? Great.

Shall we try some booleans now?
First lets establish a baseline

Ok, booleans seem ok, let’s try something else.

Really? true AND NaN produces NaN instead of a boolean?

Don’t you love Javascripts consistency? I sure do.
Let’s try something weird.

So from this one would assume that true is equal to 1 right? Let’s check that

How dare you make assumptions in language which the only thing it is consistent about is its inconsistency?

Ok, I am done for today. But in no way these are the only weird things with Javascript. No sir, there are hundreds more stupid things. I am just tired of looking for them for today 🙂
I must probably will do a round 2 some time in the future.

Excercise for home, can you find out what the following will output and why without running it?

Let me know in the comments.

Please follow and like us:

Leave a Reply