Timidger
Timidger

Reputation: 1209

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

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?

Upvotes: 3

Views: 225

Answers (1)

matt
matt

Reputation: 535191

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.

Upvotes: 2

Related Questions