Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
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 2 '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? jsfiddle.net/zVM9M/2 –  Daniel Attfield Oct 2 '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 2 '12 at 15:21
1  
jsfiddle.net/AwnVN –  Dev Oct 2 '12 at 15:27
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 2 '12 at 16:27

3 Answers 3

up vote 1 down vote accepted

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');
share|improve this answer
    
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 2 '12 at 15:43

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

share|improve this answer
    
Can you expound on that? –  Mark Rummel Oct 2 '12 at 15:02

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

http://jsfiddle.net/AwnVN/

share|improve this answer
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: jsfiddle.net/8sUhe –  Daniel Attfield Oct 2 '12 at 15:15
    
Oh, that makes difference when we define a variable as undefined. In this case no JS ERROR. Cool. –  Dev Oct 2 '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 2 '12 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.