1. Don't ever use ==
operator
It doesn't do what you think and it's quite close to be totally useless (for example "1" == [[1]]
). Prefer ===
instead. If the type is the same for both sides ==
and ===
do the same, but if they're not ==
does crazy conversions you will regret.
2. ===
for arrays checks identity
I.e. it will return true
only if the two sides are the very same object, not an object with the same content (whatever 'same' is meant to be).
If you want to check the content you should first decide how to compare elements... for example
my_eqtest([1, [2, 3]], [1, [2, 3]])
should return true
or false
?
x = [1, 2]
y = [1, 2]
y.myextramember = "foo"
my_eqtest(x, y) // should be true or false?
You should describe (document) what you mean for equality if it's not object identity, otherwise who reads the code will not understand why something is not working (including yourself in a few weeks).