0

This question is related but is not the same as javascript: get a function's variable's value within another function & How do i pass variables between functions in javascript

I have been wondering for some time now how to pass a variable from one function to another, which answer can be found above. But the bigger question is how to pass multiple variable, sense the syntax of the answers found above can become overly complex, and it ends up being unreadable.

Don't mind the adding and alerting that I am doing in the example. This is a dummie example as stated below the exaple. The purpose of this question is clearly stated in the paragraph below in bold.

So here is my question, How do you pass multiple variable from one function to another, example:

function definingVars() {
  var one = 1;
  var two = 2;
  var three = 3;
  var four = 4;
  var five = 5;
  var six = 6;
  var seven =  7;
  var eight = 8;
  var nine = 9;
  var ten = 10;
}

function addingUp() {
  definingVars();
  alert("Alerting var total");
  var total = one + two + three + four + five + six + seven + eight + nine;
  alert(total);
  alert("Alerting var ten");
  alert(ten);
}

I know that it is not the correct way to approach this. But I'm trying to make a dummie example here.

Community
  • 1
  • 1
Julian Avar
  • 476
  • 1
  • 5
  • 24

3 Answers3

5

Return an object whose properties contain the desired values:

function definingVars() {
  return {
    one: 1,   two: 2,     three: 3,   four: 4,   five: 5,
    six: 6,   seven: 7,   eight: 8,   nine: 9,   ten: 10
  };
}
function addingUp() {
  var data = definingVars();
  return data.one + data.two + data.three + data.four + data.five
         + data.six + data.seven + data.eight + data.nine;
}

If you want to avoid repeating data, you can use with, but note it's slow, bad practice and not allowed in strict mode:

function addingUp() {
  with(definingVars()) {
    return one + two + three + four + five + six + seven + eight + nine;
  }
}
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • `If you want to avoid repeating data` - see my answer :p – Jaromanda X Oct 31 '15 at 22:19
  • @JaromandaX I'm afraid that your solution does not apply to my question since you say that "If the "variable" names are not fixed, you can do the following", which is not what I'm doing, as you can see I'm doing different things with 1-9 and with 10. – Julian Avar Oct 31 '15 at 22:22
  • yes, I see that now that you've pointed it out :p - edited to "exclude" keys – Jaromanda X Oct 31 '15 at 22:23
  • In ES6 you can use destructuring instead of "with". E.g. with definingVars function which returns object like in this answer you can assign variables like: var {one, two, three} = definingVars(); – madox2 Oct 31 '15 at 22:24
1

If the "variable" names are not fixed, you can do the following

function definingVars() {
  return {
    one: 1,
    banana: 2,
    chair: 3,
    cucumber: 4,
    five: 5,
    six: 6,
    salmon: 7,
    eight: 8,
    nine: 9,
    ten: 10
  };
}
function addingUp() {
  var data = definingVars();
  var total = Object.keys(data).filter(function(key) {
      return key !== 'ten';
  }).map(function(key) { 
      return parseInt(data[key], 10); 
  }).reduce(function(a, b) { 
      return a+b; 
  });
}
  • use Object.keys to get an array of keys from the object
  • use Array.filter to exclude any keys from the addition
  • use Array.map to map the keys to the values
  • use Array.reduce to loop through the values adding them together
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
1

Or you can do something like that:

 function definingVars() {
   var one = 1;
   var two = 2;
   var three = 3;
   var four = 4;
   var five = 5;
   var six = 6;
   var seven =  7;
   var eight = 8;
   var nine = 9;
   var ten = 10;
   return [one, two, three, four, five, six, seven, eight, nine, ten];
 }

 function addingUp() {
   var sum = definingVars().reduce(function(a, b) {
     return a + b;
   });
   return sum;
 }

 addingUp();
MilenaM
  • 21
  • 2