Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  89] [ 7]  / answers: 1 / hits: 127884  / 11 Years ago, wed, october 30, 2013, 12:00:00

I'm self-teaching myself JavaScript and out of curiosity I'm wondering what is the proper way of returning a value from one function to be used in another function. For example:



function firstFunction() {
// do something;
return somevalue
}


So how do I set up the second function to use somevalue? Thanks.


More From » javascript

 Answers
3

Call the function and save the return value of that very call.



function firstFunction() {
// do something
return testing 123;
}

var test = firstFunction(); // this will grab you the return value from firstFunction();
alert(test);


You can make this call from another function too, as long as both functions have same scope.



For example:



function testCase() {
var test = firstFunction();
alert(test);
}


Demo


[#74624] Tuesday, October 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;