I have a following code:
var obj = {
car: "volvo",
speed: 20
};
var x = obj.speed;
obj.speed = 500;
console.log(x);
console.log(obj.speed);
obj.speed = 1000;
var y = obj.speed;
console.log(y);
One would expect that x would be 500 at the time you log it to console, however it is still 20. Console.log(obj.speed) results in 500.
Can you let me know why it is like that?
I understand that if I swap theier places: var x declaration and obj.speed = 500, it will point to 500. Just like with y variable
But why? Does the code not check last value of x variable before logging it?