How does using a return clause before a recursive function call differ from not using one?

前端 未结 6 454
旧时难觅i
旧时难觅i 2021-01-25 09:39

I was just experimenting with some recursion and noticed something that confused me. Let me illustrate with some code examples:

function loop(x) {
  if (x >=          


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-25 10:12

    If x<10 the loop function doesn't return any value, that' why return loop(x + 1); gives you "undefined". As soon as you reach 10, the return x; statement kicks off and you get your returned value.

    The fact that you use it as a recursive function doesn't make any difference here.

提交回复
热议问题