1

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);

Mark Rummel
  • 2,920
  • 10
  • 37
  • 52
  • You appear to be trying to break encapsulation by changing the bindings in another higher scope. Why? Even if there is a solution, this isn't good software practice. – Francis Avila Oct 02 '12 at 15:01
  • I agree with Francis, but there's nothing wrong with creating functions to modify objects or variables. You should just be explicit about what you pass in, and what you return. Maybe something like this? http://jsfiddle.net/zVM9M/2/ – Daniel Attfield Oct 02 '12 at 15:02
  • @FrancisAvila I'm getting the value of multiple checkboxes via jQuery. The ones that are checked = 1 the ones that aren't checked are `undefined`, but I need them to be `null`. For example: checkbox1 = 1, checkbox2 = 1, checkbox3 = null. – Mark Rummel Oct 02 '12 at 15:21
  • 1
    If you represent all the checkboxes as an array, you can pass the entire array to your makeNull function, and it can set the undefined elements to null. – Barmar Oct 02 '12 at 16:27

3 Answers3

2

If you are trying to define a variable with a function parameter then you either want to use an array or an object.

Jordan Denison
  • 2,649
  • 14
  • 14
1

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.

function makeNull(obj, label) {
  if (obj[label] === undefined) {
      obj[label] = null;
  }
}
var varB = { abc: undefined }
makeNull(varB, 'abc');
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I think I totally lost it here for a moment. Of course a variable as a parameter is passing whatever the variable equals, not the variable name. Everyone's answers have been very educational and I really appreciate them, but maybe I should just delete this question, because I think it is probably pretty confusing. Thanks for your answer! – Mark Rummel Oct 02 '12 at 15:43
-1

you can pass a variable (defined as 'undefined') to a function and set its value.

http://jsfiddle.net/AwnVN/

Riz
  • 9,703
  • 8
  • 38
  • 54
  • 1
    There's nothing wrong with passing an undefined variable to a function. If you're not coding defensively, it may break your weak code, but that's not a JavaScript problem - it's yours. Check this Fiddle for proof: http://jsfiddle.net/8sUhe/ – Daniel Attfield Oct 02 '12 at 15:15
  • Oh, that makes difference when we define a variable as undefined. In this case no JS ERROR. Cool. – Riz Oct 02 '12 at 15:17
  • You have to pass all arguments to function. If you dont, they will be turned to undefined implicitly. But nothing is wrong on explicitly passing `undefined`. – Zaffy Oct 02 '12 at 15:18