简体   繁体   中英

what to use return false or if else inside a function ? : java script

function abc(val) {
    if (val) {
        console.log("woo");
        console.log("woo i am in")
    }
}

abc();


function abc(val) {
    if (!val) {
        return false;
    }
    console.log("woo");
    console.log("woo i am in")
}

abc();

which is better way of above two function ? Is it the return false the right way ? i am not sure which to use and which not.

Both ways are possible and absolutely right.

If you have a large method I would prefer the second one, because then the code is more readable I think.

If you want to check if console logs were made, or anything else you do in the method, in your main code you have to return something. You return undefined when console logs are made and false if they are not made. In the main code you only have to mention that you have to check it like this:

typeof returnVal === undefined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM