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);
koc
  • 5
  • 4

2 Answers2

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);
vaso123
  • 12,347
  • 4
  • 34
  • 64
0

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

window[name] = value;
Saravanan
  • 515
  • 3
  • 14