DEV Community

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

Collapse
foxy4096 profile image
Aditya Priyadarshi

wait what is === and == and = in js
three equal to sign is very confusing

Collapse
sumusiriwardana profile image
Sumudu Siriwardana Author

Jon has explained it very simply!

It might help you to get a basic idea of JS operator with this article
dev.to/sumusiriwardana/beginners-g...

Collapse
jonrandy profile image
Jon Randy

= is assignment
== is equality (no type checking)
=== is strict equality (type checking)

My explanation here is greatly simplified - the rules for equality when using == are quite complex

Collapse
pinotattari profile image
Riccardo Bernardini

the rules for equality when using == are quite complex

Alas, that is one of the worst sin of JavaScript. == is not even transitive, which is a basic property that you would expect from equality. It is not just an academic matter, some algorithms (e.g., sorting) make the implicit hypothesis that equality is transitive and they can break down if it is not. The reason why we do not observe many break downs is that in sorting procedures data are of the same type and this "feature" of JS == (but also PHP has the same problem) does not manifest itself.

Collapse
Sloan, the sloth mascot
Comment deleted
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 on
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.