Abhishek Kumar
Abhishek Kumar

Reputation: 13

Getting return value of inner function in javascript

Here is my code i want alert to give (some data) value. which is of inner function it give undefined value

alert(f1());
function f1(){
  f2(function (){
    return "some data";    
  });
}
function f2(f4){
  // some code
}

visit : https://jsfiddle.net/dvdhyttr/8/

i want alert get value (some data). but getting undefined.

Upvotes: 0

Views: 212

Answers (2)

Ele
Ele

Reputation: 33726

Two main issues

  • Missing return within the function f1
  • Within function f2 you need to return the result of calling the function f4.

alert(f1());

function f1() {
  return f2(function() {
    return "some data";
  });
}

function f2(f4) {
  return f4()
}

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370739

You need to assign the return values of the functions to variables in the outer function and return them, all the way up to alert(f1());.

alert(f1());

function f1() {
  const resultOfF2 = f2(function() {
    return "some data";
  });
  return resultOfF2;
}

function f2(f4) {
  return f4();
  // some code
}

Upvotes: 0

Related Questions