0

I want to change value of global variable within a function with parameters name,value. All examples that ive read was with no parameter function. Example

var one = 100;
var change = function(name,value){
// name is the name of the global variable
//value is the new value
};

change(one,300);
1
  • I assume change(one,300); should be change("one",300); Commented Dec 9, 2014 at 15:37

2 Answers 2

1

Try this. When you pass it as a parameter, pass as string, not the variable.

var one = 100;
var change = function(name, value) {
    window[name] = value;
};

change('one', 300);
console.log(one);
Sign up to request clarification or add additional context in comments.

Comments

0

All global variable are set on window object. so you can try the following .

window[name] = value;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.