Strange JS (3 Part Series)
Now, Coercion is of three types. Curious? let's discuss it.
Let's understand with the example
console.log('100' - 10)
What do you think, what will be the result of the above code? if you think it will produce any error wait, my friend, you are wrong here because the output will be 90. Why?
Ok, you got the point now let's try another example
console.log('100' + 10)
Notice the (+) sign instead of (-) in the above code. So what do you think? what should be the output?
If you think it will be 110, sorry to say but you are wrong again, It will be 10010, why?
This one is most trouble maker among all, It is mainly used in if statements, loops and in logical operations.
So, let's consider this example
console.log(true + 100)
console.log(false + 100) // obiously 100
In case you didn't get it. False will be considered as 0 so 0 + 100 will be 100.
We are towards the end of this post and I hope you got the idea. Now let's take a look at the last one.
console.log('100' == 100)
console.log('100' === 100) // False
In the case of triple equal, the result will be False because triple equal operator strictly checks the type of left and right side.
I hope you got all my point. I will be back with new post until then Good Bye.