17

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.

Flip
  • 6,233
  • 7
  • 46
  • 75
GeekByDesign
  • 416
  • 2
  • 4
  • 12

7 Answers7

37

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

Ben Thomas
  • 3,180
  • 2
  • 20
  • 38
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Why not just `alert(firstFunction())`? I only ask because I marked somebody's question today as a duplicate of this one, but one of the answers there was essentially in the form I give, no intermediate variable required (or `secondFunction(firstFunction)` since `secondFunction`, a non-built-in, is mentioned in another answer) – Dexygen Dec 23 '17 at 02:56
  • 1
    That will obviously be better but note that when the question was asked the OP was a beginner and i find it better to teach beginners very basic clear concepts. Saving a return val in a variable makes sense to a beginner – Hanky Panky Dec 23 '17 at 04:31
  • OK, thanks for your answer, wasn't trying to stir anything up. I guess one other reason for your approach is it can make debugging easier – Dexygen Dec 23 '17 at 11:02
  • In java we used to mention the data type near the signature doesn't required in JavaScript? Even thought we return the value in same class or in different class? – Ashok kumar Ganesan Apr 02 '20 at 23:45
3

You could call firstFunction from secondFunction :

function secondFunction() {
    alert(firstFunction());
}

Or use a global variable to host the result of firstFunction :

var v = firstFunction();
function secondFunction() { alert(v); }

Or pass the result of firstFunction as a parameter to secondFunction :

function secondFunction(v) { alert(v); }
secondFunction(firstFunction());

Or pass firstFunction as a parameter to secondFunction :

function secondFunction(fn) { alert(fn()); }
secondFunction(firstFunction);

Here is a demo : http://jsfiddle.net/wared/RK6X7/.

0

Call function within other function :

function abc(){    
    var a = firstFunction();    
}

function firstFunction() {
    Do something;
    return somevalue 
}
Ankit Tyagi
  • 2,381
  • 10
  • 19
0

You can do this for sure. Have a look below

function fnOne(){
  // do something
  return value;
}


function fnTwo(){
 var strVal= fnOne();
//use strValhere   
 alert(strVal);
}
nrsharma
  • 2,532
  • 3
  • 20
  • 36
0
var captcha = '';
//function name one
function one(captcha){
var captcha = captcha;
//call another function and pass variable data
var captcha = firstFunction(captcha); 
};
// second function name
function firstFunction(captcha){
alert(captcha);
}
Nazmul Haque
  • 720
  • 8
  • 13
  • 4
    Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Oct 15 '20 at 08:28
0

function reSult(num1,num2){

if(num1<=num2){
    
    return num1 + num2
}
else
{
    
    return num1 -num2
}

}

let pass= reSult(40,5);

function outPut(pass,num3){ if(pass<=40){ return pass * num3 } else{ return pass / num3 }

} console.log(outPut(pass,20))

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 12 '23 at 11:21
-1

To copy the return value of any javascript(in chrome console), we can use inbuilt copy() method.

you can use any expression, function, etc find some examples below

  1. using expresseion

a = 245; copy(a);

  1. using function
a = function() {
  return "Hello world!"
}
copy(a());

Official Doc for reference

Anand Raja
  • 2,676
  • 1
  • 30
  • 35
  • 1
    Just from your source: "Warning: These functions only work when you call them from the Chrome DevTools Console. They won't work if you try to call them in your scripts." – Olafant Feb 23 '22 at 11:50
  • 1
    Yes, it'll work only in chrome console. – Anand Raja Mar 01 '22 at 16:36