0

The following function returns undefined. Why?

function noReturn(arg) {
  arg += " bar";
  if (arg.length <= 100) noReturn(arg); 
  else {console.log(arg); return arg;}
}

noReturn("foo");
//logs in the console: "foo bar bar bar bar....", but returns undefined

EDIT: ouch... of course! Thank you for your answers, all of them were perfect.

7
  • 1
    I got the answer as you wished my friend, 'function noReturn(arg) { arg += " bar"; if (arg.length <= 100) { noReturn(arg); } else { console.log(arg); return arg; }' and ' noReturn('sal')' the result was sal bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar bar Commented Jan 4, 2017 at 4:12
  • @SalindaKrish thanks for your attention! But all the answers below are correct. In your answer you made the same mistake I made, you also forgot to put return noReturn(arg). Without this return expression the code won't work. But thanks for trying to help! Commented Jan 4, 2017 at 4:19
  • 1
    No my friend putting a return statement there is not necessary at that situation,,, it will recursively call the function again..it will return when it reach the else statement thats how you get the expected out put... To be honest it worked for me Commented Jan 4, 2017 at 4:22
  • 'function noReturn(arg) { arg += " bar"; if (arg.length <= 100) { noReturn(arg); } else { console.log(arg); return arg; } ' Commented Jan 4, 2017 at 5:53
  • @SalindaKrish no, my friend, you are mistaken;) you made the same mistake as me. Try removing the "console.log(arg)" command. You are possibly being deceived by the console logging arg. Proof: run this statement: console.log(noReturn('sal')+'foo'). This will output the string "undefinedfoo", instead of sal bar bar bar.... Commented Jan 4, 2017 at 6:08

3 Answers 3

6

The function doesn't return anything because you've defined it to not return anything for inputs whose length is <= 100.

function noReturn(arg) {
  arg += " bar";
  if (arg.length <= 100) noReturn(arg); 
  //                    ^---- no return statement
  else {console.log(arg); return arg;}
}

If you want the result from recursion to propagate back, you must add the return statement.

Sign up to request clarification or add additional context in comments.

5 Comments

Right on. You have to think of it as a nested chain. The first run will call another function, but that never has a return to send it back.
@tsutton It will have a return statement when the if statement reach the 100 limit..isn't it.??
@nem035 function noReturn(arg) { arg += " bar"; if (arg.length <= 100) { noReturn(arg); } else { console.log(arg); return arg; } ,with noReturn("sal")..I execute this one and got the earlier result what he intent to take,,, how it could be possible then,,,, as sal bar bar ...
@SalindaKrish try running the function without the console.log part, perhaps you're mistakenly taking that as the return value.
yes brother thanks a lot... oho,,,,i really mistaken it.,...thanks,.,.
1

You have to do a return noReturn(arg); . Otherwise you are just recursively calling it without returning the value.

Comments

1

Because you're never returning from the initial function call.

function noReturn(arg) {
  arg += " bar";
  if (arg.length <= 100) return noReturn(arg); 
  else {console.log(arg); return arg;}
}

noReturn('foo')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.