In swift there seem to be two equality operators: the double equals (==
) and the triple equals (===
), what is the difference between the two?
== is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.
The === operator means "is exactly equal to," matching by both value and data type. The == operator means "is equal to," matching by value only.
Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other. On the other hand, Triple Equals ( === ) does not perform type coercion.
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
!==
and ===
are identity operators and are used to determine if two objects have the same reference.
Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance.
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With