DEV Community

Discussion on: If-Else or Switch-Case: Which One to Pick?

Collapse
 
pinotattari profile image
Riccardo Bernardini

I do not have the possibility of checking it, but if I remember correctly

"1.200" == "1.2"
Enter fullscreen mode Exit fullscreen mode

is true because since both sides "look" like floating point numbers, they are converted both to float before doing the comparison.

Thread Thread
 
peerreynders profile image
peerreynders • Edited
console.log('1.200' == '1.2'); // false
console.log(1.2 == '1.2'); // true
console.log('1.200' == 1.2); // true
Enter fullscreen mode Exit fullscreen mode

MDN: Equality (==) — Comparison with type conversion

  • If the operands are of different types, try to convert them to the same type before comparing:
    • When comparing a number to a string, try to convert the string to a numeric value.
    • If one of the operands is a boolean, convert the boolean operand to 1 if it is true and +0 if it is false.
    • If one of the operands is an object and the other is a number or a string, try to convert the object to a primitive using the object's valueOf() and toString() methods.