Why don't variables that point to other variables "update" when they change?

Need some help understanding variables here :slight_smile:

Out of curiosity, I decided to try and see what happens to a const variable if you update the object property it’s pointing to with a new value.

const = object.property;
object.property++
console.log(const)

The const variable didn’t change, so I thought maybe changing it to let would work, but it had the same result. Why is this the case? Do variables “hold” onto the current value of other variables at the time of declaration?

Here’s my full code:

const crewCount = spaceship.numCrew;  
let crewCount2 = spaceship.numCrew;

console.log(`crewCount: ${crewCount}`);               // crewCount: 5
console.log(`crewCount2: ${crewCount2}`);         // crewCount2: 5


spaceship.numCrew++
console.log(`Incrementing spaceship.numCrew`);

console.log(`spaceship.numCrew: ${spaceship.numCrew}`);          // spaceship.numCrew: 6
console.log(`crewCount: ${crewCount}`);                                          // crewCount: 5
console.log(`crewCount2: ${crewCount2}`);                                     // crewCount2: 5

https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/object-property-access

they don’t point to other variables

Er, okay - can you point me to a source / explainer?

you asked why they don’t update, that’s why they don’t update
not sure what explanations remain. got a question?

If you throw a bunch of kids in a pool, then you can yell the name of a specific kid to get their attention. If you tell somebody else about a kid, then it’s not you who gets copied, and it’s not the kid that gets copied. What gets copied is the kid’s name. If you paint jimmy blue and tell greg about jimmy, then greg can also look at jimmy and see that jimmy is indeed blue.
If you really want to copy a kid, bring them to a cloning facility, but otherwise they don’t spontaneously duplicate.
Feel free to name a kid several names, as well. You can sign up your kid to school, they’ll be entered into some kind of list, and this will also not copy the kid, it’ll use the kid you already have.

1 Like

Okay, you’re actually making this more confusing. If variables hold a value, and variables can reference other variables, shouldn’t a variable be able to change when the source variable changes?

i.e. if

var a = var b
var b = value

then 

var a = var b = value

It seems logical to me that a variable dynamical updates, i.e. when it goes looking for var a, it finds a reference to var b, then it goes looking for the current value of var b rather than the value at initial declaration of var a.

> a = b = 42
 > a
<- 42
 > b
<- 42

Both variables point to the same value, not to each other.

 > a = 6
 > b
<- 42

A variable references a value.
A variable is not a value.
A variable can not reference a variable.

A variable is a name for a value.