Jump to content

Variables equal to another variable


buyerguy5

Recommended Posts

Hi, I am a novice at javascript and I am hoping the forum can help me out with a question:Given the following code:var x = 8;var y = x;var x = x+4;alert(y);The result is y=8Meaning, when you change the value of x (after the statement y=x), y doesn't correspond to x anymore.Is it possible to have y change with x all the time- is this doable?I thought in other languages, if you do y=x, it will permanently associate y with x so that every time x changes, so does y.How can this be done in javascript?Thanks

Link to comment
Share on other sites

You're assigning only the value of x to y. If x changes afterwards y is not affected. The one thing you can do is some kind of function reference.

var x = 8; var y = function() { return x; } x = x + 4; alert( y() );

People would call what you're asking for "syntactical sugar"

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...