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 bee overly plex, 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);
}
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 bee overly plex, 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.
Share Improve this question edited May 23, 2017 at 11:58 CommunityBot 11 silver badge asked Oct 31, 2015 at 22:03 Julian AvarJulian Avar 4761 gold badge5 silver badges24 bronze badges 2-
5
You could/should return an array
[1,2,3 ...]
or an object{ one:1, two:2, three: 3 ...}
fromdefiningVars
– Jaromanda X Commented Oct 31, 2015 at 22:07 - @JaromandaX: s/could/should/ – Bergi Commented Oct 31, 2015 at 22:09
3 Answers
Reset to default 5Return 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;
}
}
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
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();