简体   繁体   中英

How to assign a Javascript variable to the same value of another variable

How do you make a Javascript value have the same value as another variable. I have a variable called "buttonId", and another called "toWin", I want to make it so that buttonId's value doesn't change, but, toWin changes to have the same value as buttonId. something like:

toWin=buttonId;

Can anyone help me? No jQuery, please.

Just like you have it, but if you're declaring it for the first time, use the var keyword.

var toWin = buttonId;

If buttonId is an object, you may want to clone the object before setting toWin to the value of buttonId to avoid changing the value of the original object ( buttonId ) when and if you change toWin .

You forgot to mention var while declaring the second variable. you should do it like this:

var something = '1'; 
var anotherthing = something;

alert(anotherthing);

The output will be 1.

This works:

toWin=buttonId;

I was debugging my Javascript, and thought that this might be the problem, so I just checked on here if it works, but now I know that my problem is completely different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM