Double equal comparison in Javascript is quite different than other programming languages because of The Javascript Weakly-Typed. If you work with strongly-typed language for a long time this thing can be hard to understand for you. Javascript seen as uncontrolled and irregular. Many of memes and coding challenges in internet support this decision.
0 == "0" // true
If you say something like this:
In Javascript double equals only checks values not types.
Yes, you are right but we will understand how works Javascript in these cases.
This code works like this:
0 == Number(“0”) // true
In these situations, Javascript engines run this algorithm Abstract Equality Comparison Algorithm (cool name).
When you use **== **this algorithm runs step by step until the result is found.
Our case stopped in the fourth step. It means:
If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
When our case different like this:
0 == "1" // false
The result will be false because of the converted value is not equal to “0”.
If you want to have some fun check this out:
0 == “0000” // true
// Because Number(“0000”) returns 0
#javascript #comparison #programming