1

I have a situation as follows.

var varable_1=10;
var variable_2=20;
.....
var variable_n=10000;
function update_varable(variable){
   ....some code.....
}

I need to update each of those variables by calling update_variable('variable_1');update_variable('variable_2')....etc.

Is it possible?

3
  • 2
    Is there a reason why you don't simply use an array like var variables = [10, 20, 30, /* , ... */ , 10000]? Then you could just give update_variable the index i and use variables[i]. Commented Jul 9, 2013 at 6:38
  • Can you put more details in your question? As I understood, you're trying to loop through all the variables in a function? Commented Jul 9, 2013 at 6:39
  • sorry for the inclonvenience. actually i neeed to implement a function for updating different variables. For illustrating it i use variable_1, variable_2 etc... Commented Jul 9, 2013 at 6:43

5 Answers 5

2

If you want to pass the variables inside the function update_variable then you need to remove the quotes in your example. There is many ways to do it, I post a simple one. You can also pass more than one variable inside the function.

Demo here

var varable_1=10;
var variable_2=20;
var variable_n=10000;
function update_variable(x){
x = 300 //some new value
return x;
}

and the call:

variable_1 = update_variable(varable_1);

( your function name misses an "i" on some lines, it's "update_varable" )

                                                   ^  
                                               missing "i"
Sign up to request clarification or add additional context in comments.

4 Comments

var is keyword. So no problem? Is it work as pass by reference?
@ManeeshMohanachandran, It's already corrected, var cannot be used as variable, of course. Thanks, got an early morning :)
@ManeeshMohanachandran, i added a live example also in my answer.
got the right answer. Thank you. 'i' missed when i typed it into the stack. :)
1

If you have to use a string as argument for the update function, you can use eval inside of the function to get the real variable behind the string:

function update(varName) {
    eval(varName + " += 1;");
}

Comments

1

I think array is more suitable for the task.

But you can use this code with eval function if your varaibles names are like var1, var2 .. varN:

    var var1 = 10;
    var var2 = 20;

    function update_var(variable) {
        return variable += 1;
    }

    function main() {
        for (var i = 1; i < 3; i++) {
            eval("var" + i + " = update_var(var" + i + ")");
            eval("console.log(var" + i + ");");
        }
    }

Comments

1

Let's see the facts here:

  • Your variables have a pattern: variable_x so, for our algorithm it s just a string: 'variable_' + x
  • All your variables will be attached to an object, wetter it is a declared object, or a global one, for example: a, myVars, or window
  • Any object in javascript can be accessed using indexers, so myObject.myVar can be also written like myObject['myVar'].

Now let's see the algorithm:

function update(variable, value){
        window[variable] = value;
}

You can call it like you wanted:

update('variable_1', 450.25);

Comments

-1
function update_varable(variable){
   variable += 10;

return variable;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.