TheChosenWhanMC TheChosenWhanMC - 3 months ago 10
Javascript Question

How to change a variable that was called in parameters JavaScript

Im working on a project, and in an attempt to shorten the amount of code I wrote a function to call all of them at once. It looks something like this

function Function(peram1) {peram1 = peram1 += 5};
And I call it like so
My issue is that I need to change the variable someVariable from inside the function, even though the value of peram1 is simply just the value of someVariable, but cant directly change it. I call multiple of these functions, so I cant simply just call the actual variable name from within.

var someVariable = 5;
var someSeperateVariable = 8;

function Function(peram1) {
peram1 = peram1 += 5;

console.log(someVariable, someSeperateVariable);


You can use an object to store variables, pass property name and object to function

var obj = {someVariable:5, someSeparateVariable:8};

function fn(prop, obj) {
    obj[prop] += 5;

fn("someVariable", obj);
fn("someSeparateVariable", obj);
console.log(obj.someVariable, obj.someSeparateVariable);