As the title indicates, I want to modify a global variable from a function without accessing it directly, thus:
var a = 5;
console.log(a);
midificaVariable(a);
console.log(a);
function modificaVariable(b)
{
b--;
}
the variable a in the first log would have a value of 5 and in the second log a value of 4.
The idea of this is to use a function to edit different variables regardless of their name.
If the variable a
is an object or an array, when it is put as an argument a reference is created, meaning that if I modify the reference, I am modifying the global variable.
If what the "modificaVariable" function does is modify "a", the simplest and fastest thing that comes to mind is that you return the value it should have, that is, something like this:
will show on the screen
In order to execute the function, yes or yes you will know what the global variable is called, so this example function would adapt to the requirement, since it never knows the name of the variable.