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.

> '5' - 3
< 2

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.

> '5' + 3
< "53"

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?

> '5' - '3'
< 2

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

Ok now let's try something else.

> '5' + + '4'
< '54'

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

> 'foo' + + 'bar'
< 'fooNaN'

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

> + '4'
< 4

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

> '5' + -'4'
< '5-4'

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

> var x=3
< undefined
> '5' -x + x
< 5
> '5' + x - x
< 50

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

Let's move one to other stuff.

> typeof NaN
< "number"
> NaN == NaN
< false

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.

> x=[0]
< [0]
> x == x
< true

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

> x == !x
< true

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

Ok. What about the following?

> Array(3)==",,"
< true

Great, nice, awesome....

So what is null really?

> typeof null
< "object"

Ok, so which means

> null instanceof Object
< false

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

> "string" instanceof String
< false

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

> Math.max() > Math.min() 
< false
> Math.max() < Math.min()
< true

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

> true == false 
< false
> false == false
< true
> true == true
< true

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

> true && false
< false
> true && true
< true
> true && NaN
< NaN

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

> false && NaN
< false

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

> true + true === 2 
< true 
> true - true === 0 
< true

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

> true === 1 
< false 

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