I'm needing to compare the previous state of an array length with a later state (not referring to state syntax). I thought the task was as simple as assigning the variable to a new variable at the point in time you want to capture, then just use that variable to compare with the main variable later, i.e;
let x = [1, 2];
const y = x;
function test() {
x.push(3, 4);
console.log(x.length > y.length);
}
test();
But for some reason this returns
false
I only have this issue when using push()
. As soon as x.push(3, 4)
is executed for some reason y
is updated to have the same values as x
after pushing in more data. This confuses me because they're in different scopes and executed at different times? Is there something magical I don't understand about push()
?