how to reassign the function argument in JavaScript

Viewed 50

The challenge & explanation.

I want to reassign a global variable when it's passed to a function as an argument but here is the catch I don't want to reference it directly inside of the function so here is the result I want to occur.

let number = 5;

function changesVariable() {
  number = 3;
}
changesVariable();
console.log(number); // 3

but I don't want to directly add "number" inside of the function I want a behavior like below I want to pass "number" as an argument and then the function reassigns it by that.

let number = 5;

function changesVariable(n) {
  n = 3;
}
changesVariable(number);
console.log(number); // here it returns 5 but would like to see 3

BTW I know the above code is wrong it's here only to explain the concept.

How I tried to solve it

so below are some examples that how I approach it. but their arent the answer.

function changesVariable(n) {
  arguments[0] = 3;
}
changesVariable(number);
console.log(number); // still 5

we can do something like:

function changesVariable(n) {
  return (arguments[0] = 3);
}
number = changesVariable();
console.log(number);

but I am seeking an answer with these conditions:

  1. reassign the "number" by passing it to function as an argument.
  2. I don't want to use "number" variable directly inside the function.

If all the text above didn't help

I want to create a Utils function that you can pass different global variables to it and it will reassign them that's why I have this approach I want the function to be reusable like:

changesVariable(number1);
changesVariable(number2);
changesVariable(number3);
changesVariable(number4);

4 Answers

I think with primitive it's not possible cause it's not a reference data type. Try to put it into an object like: {n: 5}, and pass it like this.

let number = {n:5};
function changesVariable(number) {
   number.n = 3;
}
changesVariable(number);
console.log(number.n); 

This is going to work because it's not a primitive, but more like a reference type with a primitive in it. So the function parameter and the global variable will be pointing to the same memory address. More info: https://www.tutorialspoint.com/What-are-reference-data-types-in-Java#:~:text=Reference%20datatypes%20in%20java%20are,an%20object%20of%20a%20class.

JavaScript is a pass-by-value language, i.e. functions are being passed a copy of the value (not to be confused with object references). I.e. what you want isn't possible, at least not exactly like that...

I want to create a Utils function that you can pass different global variables

How about that utils function accepts a callback which accepts the new value and can do whatever it wants with that?

function change(callback) {
  callback(3)
}

//...


change(newValue => {
  number1 = newValue;
  number2 = newValue;
})

If the function needs the current value of the/a variable you can pass that in too (but your requirements are not clear).

You have a let binding in your example.

There is no accessible namespace for a let (or const, but that's beside the point here since you're talking about writing things) bound variable, so in short, what you're asking for is impossible in JavaScript.

If you had a var in global scope, you could access it via globalThis by name:

> var foo = 8;
> globalThis.foo
8
> globalThis.foo = 9
9
> foo
9

However, it sounds like you don't really even need global variables here; why not just put these variables in an object of your own?

> const things = {foo: 8};
> function modify(name, value) { things[name] = value; }
> modify("foo", 9)
> things
{ foo: 9 }

(And yes, you can use modify with globalThis too...)

You can do that with eval(), just pass the name of the global var to the function:

let number = 5;

function changesVariable(arg) {
    let a = eval(`'(${arg}) = 3'`)
    a = a.replace('(','')
    a = a.replace(')','')
    console.log(a);
    eval(a)
}
changesVariable('number');
console.log(number);

But using eval() is not recommended...

Related