The title pretty much says it all, but here it is written out:
b = [1, 2, 3, 4];
c = [...b];
b === c; //false
Why?
The title pretty much says it all, but here it is written out:
b = [1, 2, 3, 4];
c = [...b];
b === c; //false
Why?
It's how regular array identity / strict equality comparison works. Remember that arrays are objects:
The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
- If x is NaN, return false.
- If y is NaN, return false.
- If x is the same Number value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
- If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
- Return true if x and y refer to the same object. Otherwise, return false.
NOTE This algorithm differs from the SameValue Algorithm (9.12) in its treatment of signed zeroes and NaNs.
The ...
has no impact. If we assign the same literals to both we can see this:
b = [1, 2, 3, 4];
c = [1, 2, 3, 4];
b === c; //false
This is because each []
will create a new array, even if it uses a spread in it.
c
is a new Array
instance, not the same object.
You can use .every()
to check if every element at index of b
has the same value of element at index c
let bool = b.every((n, index) => c[index] === n)