A graceful face test analysis of the difference between FN () and return FN () in JS _javascript skill

Source: Internet
Author: User

In JS, often encountered in the function to call other functions, this time there will be FN () this invocation, there is a return to FN () this way of invocation, some beginners are often the two ways to get around the halo. Here is an elegant face test to analyze the difference between the two ways.

var i = 0;
function fn () {
 i++;
 if (I < ten) {
 fn ();
 } else{return
 i
 }
}

var result = FN ();
Console.log (result); 

This is a hidden pit of the face test, seemingly very simple, most people may not want to answer the 10. In fact, the operation of the print is undefined. This trap question is a straightforward reflection of the previous question, when we modify the line that executes FN to:

var i = 0;
function fn () {
 i++;
 if (I < ten) {return
 fn ();
 } else{return
 i
 }
}

var result = FN ();
Console.log (result); 

At this point, it will be found that the results of the printed result is 10.

Why is it so different from this?

The main reason here is very simple, JavaScript functions have a default return value, if the function at the end of the return, the default returned to undefined, which is why in the Chrome console console, Writing code often causes a row of undefined to appear below.

Take a closer look at this example, when I was 9, that is, the penultimate recursive invocation of FN, if there is no return, this time after the FN, the default return undefined will be returned, and the next recursion will not continue. When the return is added, the last recursion will continue here, that is, when i=10, jump in else to get the correct 10.

Here, we can draw a more classic example, the famous two-point search method :

var mid = Math.floor ((arr.length-1)/2);

function search (n, mid) {
 if (n > Arr[mid]) {
 mid = Math.floor (mid + arr.length)/2);
 Return Search (n, mid);
 else if (n < Arr[mid]) {
 mid = Math.floor (mid-1)/2);
 Return Search (n, mid);
 else {return
 mid;
 }
}

var index = search (n, mid);
Console.log (index); 

The binary lookup method also requires multiple recursive calls, many beginners in the first implementation of this algorithm often make a mistake is to forget in the recursive function plus return, the final result is undefined, the truth here is similar to the previous, without return, will lead to recursion , returning directly to undefined will not continue with the next recursion.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.