In this article I will try to demonstrate some funny / weird parts in JavaScript. Besides its flaws we are using it and we are loving it! (most of the time!)
Let’s start!!
true + truewhat do we expect from the above line of code??
Anything you are thinking right now is wrong!! Unless you had known the answer already! :)
The correct answer is: 2
wait what??
Now that we understand the craziness of this article let’s continue!
Something with strings!!
“This is a test string” instanceof String //RESULT: fasleOK, probably I can live with this!!
But wait…
typeof “This is a test string” //RESULT: stringohh nooo!!!!
Continuing with numbers!!
JavaScript loves numbers so much!!
So if you write the number
9999999999999999, JavaScript thinks that is the same as 10000000000000000 !!! Smart right??Let’s make a simple addition floating point numbers
0.1 + 0.2 0.3 //POSIBLE RESULT: 0.3The above is right for you!! Not for JavaScript!!! For the JavaScript the result is 0.30000000000000004 !
Yes, JavaScript says “I am better in maths than you”!
3 > 2 > 1You are guessing wrong again!! The right answer is not true!! But false!
Now your brain fights to recognize reality!!
Next one
“2” + 1 // RESULT: 21
“2” — 1 // RESULT: 1Boom!! After that your brain is losing the fight!
A little bit about falsy values
typeof NaN //RESULT: NumberOK, I will pretend that is right!
NaN instanceof Number // POSSIBLE RESULT: trueNope!! It is not!! It’s false!!
And something that I just leave here
typeof null //RESULT: objectNo comment here!!
Final chapter
Arrays and objects. After this I will leave you in peace
[] + [] //RESULT: “”
[] + {} //RESULT: “[object Object]”Based on the above, are you thinking
“[object Object]”? Nope{} + [] //RESULT: 0The purpose of this article is to mock some funny aspects on our every day routine. However JavaScript is one of the most usable languages. Millions developers use it every day.
