JavaScript - The == does coercion

javascript

// JavaScript - The == operator does implicit coercion.

Consider:
[]==true

When the left and right side of the equality are two different types, 
JavaScript can't compare them directly . Hence, under the hood, JavaScript will 
convert them to compare. first right side of the equality will be cooereced to 
a number and number of true would be 1.  After that, JavaScript implementation 
will try to convert [] by usingtoPrimitive (of JavaScript implementation). 
Since [].valueOf is not primitive will use toString and will get "".  Now you 
are comparing "" == 1 and still left and right is not same type. Hence left side 
will be converted again to a number and empty string will be 0.  Finally, they 
are of same type, you are comparing 0 === 1 which will be false.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License