Before you go, check out these stories!

0
Hackernoon logoThe funny-weird parts in JavaScript! by@thanos-stamodimos

The funny-weird parts in JavaScript!

Author profile picture

@thanos-stamodimosThanos Stamodimos

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 + true

what 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: fasle

OK, probably I can live with this!!

But waitโ€ฆ

typeof โ€œThis is a test stringโ€  //RESULT: string

ohh 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.3

The 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 > 1

You 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: 1

Boom!! After that your brain is losing the fight!

A little bit about falsyย values

typeof NaN   //RESULT: Number

OK, I will pretend that is right!

NaN instanceof Number   // POSSIBLE RESULT: true

Nope!! It is not!! Itโ€™s false!!

And something that I just leave here

typeof null  //RESULT: object

No 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: 0

The 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.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.