Is it possible in JavaScript to define a variable name with a function parameter?
Here is my jsFiddle. In the example below, I would like varB to be defined as null when it is passed through makeNull(varName):
var varA = undefined;
if (varA == undefined) {
varA = null;
}
var varB = undefined;
makeNull(varB);
function makeNull(varName) {
if (varName == undefined) {
varName = null;
}
}
alert (varA + ' | ' + varB);
JavaScript doesn’t provide “call by reference” parameters. When you call a function, it receives the values of the argument expressions, not references to the variables that were in the expressions. So it can’t modify the bindings of those variables.
If you want to do something like this, you can use containers and labels, e.g.