Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the array use the '===' operator to compare elements?

Tags:

ios

swift

From the documentation about arrays:

Check whether two arrays or subarrays share the same storage and elements by comparing them with the identity operators (=== and !==)

However, to check if two arrays contain the same elements, wouldn't you use the comparison operator == (because you're comparing the values of the array) instead of the identity === (which would check if you are referring to the same array)?

Assuming I am understanding the operators properly, is this behaviour caused by array's implementation as a struct?

like image 473
Timidger Avatar asked Sep 17 '26 09:09

Timidger


1 Answers

The key word there is "storage" - that means they are one and the same "object", which is indeed identity.

And this check is needed because arrays are copied by reference rather than by value, so you can fall into the trap of altering "another" array if you're not careful - which is what this section of the guide is about.

like image 120
matt Avatar answered Sep 18 '26 23:09

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!