Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the current value of a variable

Tags:

javascript

I have a variable myVariable which is continuously changing. At some point I want to capture the value of myVariable (not a reference to myVariable) into another variable myVariableAtSomePoint.

Example code:

var myVariable = 1;

function test () {
    var myVariableAtSomePoint= myVariable;
    console.log(myVariableAtSomePoint);
}

myVariable = 2;

test(); // Prints 2, instead of 1.
like image 819
Randomblue Avatar asked Oct 19 '11 14:10

Randomblue


People also ask

How do you find the current value of a variable in Python?

run(init) v = sess. run(x) print(v) # will show you your variable. The part with init = global_variables_initializer() is important and should be done in order to initialize variables. Also, take a look at InteractiveSession if you work in IPython.

How do you save a value to a variable?

You store a value in a variable by putting the variable name on the left side of an assignment statement.

What is current value JavaScript?

currentValue. The value of the current element. On first call, the value of array[0] if an initialValue was specified, otherwise the value of array[1] . currentIndex.

Can you change the value of a variable?

You can "assign a new value" to your variable, but you can't change the value itself, unlike an array or object, which you can modify without affecting the variable binding.


2 Answers

You mention in your comments that myVariable is a number. Since myVariable houses a primitive type, simply use the code below:

myVariableAtSomePoint = myVariable;

Take a look at JAVASCRIPT: PASSING BY VALUE OR BY REFERENCE. Here's a quote:

When passing in a primitive type variable like a string or a number, the value is passed in by value.

I would also suggest reading: How do I correctly clone a JavaScript object?

EDIT:

I believe that you're assuming that the placement of the function in the code affects the value of the variables. It does not. See examples below:

This:

function test () {
    var myVariableAtSomePoint= myVariable;
    console.log(myVariableAtSomePoint);
}

myVariable = 2;

test(); // Prints 2, instead of 1.

Is the same as this:

var myVariable = 1;

function test () {
    var myVariableAtSomePoint= myVariable;
    console.log(myVariableAtSomePoint);
}

myVariable = 2;

test(); // Prints 2, instead of 1.

Your problem is that you're changing the value of myVariable before you're assigning it to myVariableAtSomePoint. For this to work as you want, you'll need to call the test() function before you change the value of myVariable

var myVariable = 1;

function test () {
    var myVariableAtSomePoint= myVariable;
    console.log(myVariableAtSomePoint);
}

test(); // Prints 1
myVariable = 2;
test(); // Prints 2

IMPORTANT: No matter the placement of the function, the code inside test() is not executed until you call the function.

like image 177
James Hill Avatar answered Sep 28 '22 16:09

James Hill


The variable only holds the reference and nothing else. If you want a copy of the object that the variable is pointing to, you will need some way to copy that object. Either you can implement a copy method on the object itself (if you know what properties it has) or you can iterate through all properties on the object using a for ... in loop and copy them to a newly allocated object. Something like:

//o is the object you want to copy
var o2 = {}; //gives a new object
for (x in o) {
   o2[x] = o[x];
}
like image 27
Mathias Schwarz Avatar answered Sep 28 '22 14:09

Mathias Schwarz